Thursday, May 20, 2010

Different types of Inner Classes

In java we have a option to write a class in another class.Such a class is called the Inner class.
check the following diagram ,it will explain the how the class architecture in the Java

These are categorised into Static and non-static inner classes.Under non-static inner classes we have inner classes ,Local inner classes and Anonymous Classes .Objects that are instances of an inner class exist within an instance of the outer class
and has direct access to the methods and fields of its enclosing instance
Local inner class
we can declare a inner class with in a body of a method
Syntax of creating an object for  inner class
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Anonymous inner class
we can also declare a inner class with out a name with in the body of a method.
ex:DriverManager.getConnection() method is using this concept only.
Static inner classes
As with class methods and variables, a static nested class is associated with its outer class.
Syntax of creating an object for Static inner class
OuterClass.InnerClass innerObject = new OuterClass.new InnerClass();

Ex
class OuterClass
{
   ------
   -------
       class InnerClass
          {
             -------
             -------
          }
}

0 comments:

Post a Comment