Wednesday, August 11, 2010

About innerclasses ?

There are several types of inner classes.

Inner classes
method-inner classes
Anonymus inner classes
static nexted inner classes


The usage of each inner class has shown below :


Inner class:


class Outer
{
class Inner
{
public void innerClassMethod()
{
System.out.println("You are in the method of inner class");
}
}
public void outerClassMethod()
{
Inner i=new Inner();
i.innerClassMethod();
}
}

The above represents the how to create a inner classes and how to use them. As you know when we compile a java file there will be a class files will be generated for all the classes defined in the java file. For inner classes also it will generate the class file as Outer$Inner.class.(But we can not run using Outer$Inner directly).

We can also use the innerClassMethod() in the main also (not only in outerClassMethod()) by creating the object of inner class in the main as below

Outer.Inner innerInstance= new Outer.new Inner()
innerInstance.innerClassMethod();

method-inner classes:
class Outer
{

public void outerClassMethod()
{
class Inner
{
public void innerClassMethod()
{
System.out.println("You are in the method of inner class");
}
}
Inner i=new Inner();
i.innerClassMethod();
}
}

Creating object for method inner classes:

We can only create the object of method-inner classes only in the methods in which the inner class is defined like above.

Anonymous Inner Classes :
class A{
public void sampleMethod() {
System.out.println("Sample Method");
}
}
class B{
A p = new A() {
public void sampleMethod() {
System.out.println("anonymous Sample Method");
}
};
}


If you observe class B, there is class defined with the name outer method A. We can consider that method subclass of class A. But without name. Thats why it is called as Anonymous inner classes.

Static inner classes:
class BigOuter {
static class Nest {void go() { System.out.println("hi"); } }
}
class Broom {
static class B2 {void goB2() { System.out.println("hi 2"); } }
public static void main(String[] args) {
BigOuter.Nest n = new BigOuter.Nest(); // both class names
n.go();
B2 b2 = new B2(); // access the enclosed class
b2.goB2();
}
}

Above i have defined two types of using static inner classes.

Friday, August 6, 2010

Differences between jar, war, ear files ?

jar - java archive,These files are with the .jar extension. The .jar files contain the libraries, resources and accessories files like property files.

war - web archive : These files are with the .war extension. The war file contains the web application that can be deployed on the any servlet/jsp container. The .war file contains jsp, html, javascript and other files for necessary for the development of web applications.

EAR - enterprise archive, These files are with the .ear extension.
All .jar and .war are packaged as JAR file with .ear extension and deployed into Application Server.

we can say EAR= WAR+JAR.

These three types of files called as jar files with different extensions. If we want to generate these files we will use JAR as a command in the command prompt.

OOPs Concepts ?

Object oriented Programming includes

Objects
Classes
Data abstraction and Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing

Tuesday, August 3, 2010

Default values of a instance variables ?

The default values of INSTANCE variables are

Object reference - NULL
byte, short, int, long - 0
float, double - 0.0
boolean - false
char - '\uooo' // Unicode character

How to Sort a Collection ? or About Comparable,Comparator interface ?

Here is the example of a sorting a collection ArrayList of String objects.

ArrayList stringList=new ArrayList();
stringList.add("world");
stringList.add("hello");

Collections.sort(stringList); //Collections is a utility class using that we can sort.
After this method the resultant list contains 2 strings with sorted. That means we are able to sort the ArrayList (collection) successfully.

Similarly, If we try to sort a user defined class objects instead of String objects like..
ArrayList dvdList=new ArrayList();
.........
.........
Collections.sort(dvdList); // It will not sort like Sting objects collection.
Because compiler does not know on which basis it has to sort. So, we have to tell programatically to sort the collection by implementing DVDInfo class to Comparable interface. For your information, String objects are implementing Comparable interface internally.

If we implement Comparable interface to our class DVDInfo, we have to implement or override the one of following method.
public int compareTo(Object obj) { .......} or
public int compareTo(DVDInfo obj){ .......}.

Using compareTo() we can have only one sort sequence. To want many sort sequences we use Comaparator and its method compare(). compare() method takes 2 arguments. First argument is which class is to be sorted, second is as which basis the class is to be sorted which should implement Comparator and override the method compare().

String, Wrapperclasses, Date, Calender classes are implicitly implement the Comparable interface.

Monday, August 2, 2010

Difference between String, StringBuffer, StringBuilder ?

String Objects are immutable. That means we can not change the value of a String Object. For example,

String s="Hello"; // A object creates in the heap memory and 's' refers to it.
String s2=s.concat("World");// A new object creates with the "Hello World" and s2 refers to it instead of using the same memory for "Hello" Object

So, Here there is a wastage of memory for 's' object.

Unlike Strings, StringBuffer and StringBuilder are mutable. We can change the value in these objects.
The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.