Wednesday, October 20, 2010

Inner Classes in Java

Question : What is inner Class?
Ans : A class defined in another class is known as Inner Class.
Question :What are the Inner Classes in Java?
Ans : i) Member Inner Classes
        ii) Static Inner Classes.
        iii) Local Inner Classes.
        iv) Anonymous Inner Classes.
Question :What is Member Inner Classes?
Example :
        package newPackage;
public class MemberInnerClass {
public static void main(String[] args) {

/*Creating the Inner Class Object */
MemberInnerClass.InnerClass ob = new MemberInnerClass().new InnerClass();
ob.function();
}
int outerClassNonStaticMember = 100;
static int outerClassStaticMember = 300;
class InnerClass
{
int innerClassNonStaticMember = 500;

static int v = 800;


/*Error :  1 We cannot create a static member inside a inner 

class which is not static*/
public void function()
{
System.out.println(" Inside Inner Class 'Function()' ");
outerClassStaticMember = 400;
static int myVar = 500;

/*Error  We cannot create a static member in non static block But We can access the static members from // a Non static block.*/

System.out.println("Accessing the outerClass Non Static Member : "+outerClassNonStaticMember);
System.out.println("Accessing the outerClass  Static Member : "+outerClassStaticMember);
outerClassNonStaticFunction();
outerClassStaticFunction();
}
}
public void outerClassNonStaticFunction()
{
System.out.println("Outer Class Non Static Function ");
}
public  static void outerClassStaticFunction()
{
System.out.println(" Outer Class Static Function ");
}
}
  class myClass
  {
 MemberInnerClass.InnerClass ob = new MemberInnerClass().new InnerClass();

/* ERROR  We canot create an Object for the private inner class in another class except in Outer Class. */

  }

Key Points :

1) we cannot create static members inside a non-static block.
   i.e., Error 2  
 we can access static members from a Non static block.

2) we cannot create a static member inside a non-static member inner class.
   i.e., Error 1 
  Note : A non static member inner class is just like the non static block in a class.

3)we cannot create an object for the private class in any class except in Outer Class.
 i.e., Error 3

 4) An inner class can extend any class which is available to Outer class.
       /*               Static Inner Class                 */

What is Static inner Class?
Ans :
                   An Inner Class defined as static is called a Static Inner Class.
Example : 
             package newPackage; 
public class StaticInnerClass {
int instanceVariable = 100;
        static int classVariable = 400;   
static class staticClass
{
int staticClassInstanceVariable = 500;
static int staticClassClassVariable = 600;
 
public static void staticFunction()
{  
System.out.println(" Static Function in Static Inner Class");
System.out.println("   "+instanceVariable);//Error 1 
System.out.println("   "+staticClassClassVariable);
}
public void nonStaticFunction()
{
System.out.println(" Non Static Function Inside the  Non Static Inner Class ");
System.out.println(" "+instanceVariable);//Error 1 
System.out.println("   "+staticClassClassVariable);
}
}
public static void main(String[] args) {
StaticInnerClass.staticClass object = new StaticInnerClass.staticClass();
object.staticFunction();
object.nonStaticFunction();//Point 1

}

public void nonStaticOuterClassFunction()
{
System.out.println(" NON Static Function in Outer Class");
StaticInnerClass.staticClass object = new StaticInnerClass.staticClass();
object.staticClassInstanceVariable = 500;
object.nonStaticFunction();
}
}

Important points about the Static Inner Class  


Point 1 :  A non Static function inside an static Inner class is also a static function,
                Because it is Declared in static block.


Notice point 1 in above Program.
  Observation : Here we are accessing a non-static function("nonStaticFunction")
                  where it is declared in static class from a static context in outer class.


  Point 2 : Just like previous Observations, We cannot access the non static members(instance
                  variables) from a static context.


Notice Error1 in above Program.
 Observation : Here we trying to access the instance variables from a static Context.
                         but that is not Possible we cannot access those.
Simply : Static members of a outer class are available to a static Inner class.But
            Non static members of a outer class are not available to static Inner class


Local Inner Class in Java

aWhat is LocalInner Class?
Answer : A Class declared inside a Body of a method is known as "Local Inner Class"
Example :
                    Class LocalInnerClass {
                       int x ;
                       public void function()
                      {
                          int i;
                          final int j = 30;
                          class innerClass
                         {
                               public void innerClassFunction()
                            {
                               System.out.println("Inside the local Inner Class Function");
                               System.out.println(x);
                               System.out.println(i); //Error 
                             }
                          }
                        //Creating the Object for the Local Inner class in method where it is declared.
                           LocalInnerClass lic = new LocalInnerClass();
                              lic .innerClassFunction();
                        }//End of Function
  public static void main(String[] args)
{
     LocalInnerClass ll = new LocalInnerClass();
      ll.function();
}                      
}


Important Points of the Local Inner Class.
Point 1 : Local Inner Class is just like a Local variable to class.
point 2 : As like "access specifiers are not applicable for local variables"
so,we cannot specify the access specifiers for local inner classes.


point 3 : we cannot create the 'localinnerclass' object in the outer class.because the local inner 
class we define inside the local function of the outer class,so that localinnerclass
is available to that local function only.so we can create the object of the inner local
class only in the local function of the outer class.


point 4 : A local inner class, cannot access the local variables of the method/function in which 
it is declared.it can only access the constants(like final) of that method.


 Anonymous InnerClass
What is Anonymous Inner Class?
Ans : An Anonymous inner class is just like the local inner class,but it has two differences
Difference 1 : it does not have the class name as like local inner class.
Difference 2 : As name is not there it does not have the constructor also.


Observation 1 : An Anonymous class does not have the class name
  Example :   
           {
               public void funX()
              {
                 -------------------
                --------------------
              }
              public void funY()
             {
                ...........................
                ..........................
              }
          };
  
Above is the Anonymous inner class.
Observation 2 : Inorder to define an anonymous inner class we definitely need a
 Interface or an Abstract class.
Example :
               Interface myInterface
            {
                  public void funX();
                  public void funnY();
             }
              class Anony1
{
         static myInterface my = new myInterface() {
                                                                   public void funx() {------}
                                                                   public void funy() {------}
                                                                   };
}
  class Test
{
         public static void main()
{
    Anony1.x.funx();
}
}


Example: a very common usage of an anonymous inner class

public class GUI extends JFrame{
...
public void buildComponents(){
...
button1 = newJButton();
...
button1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
...
...
}
});
...
...
}
}

Interface = ActionListener 
Anonymous inner class : 

public void actionPerformed(java.awt.event.ActionEvent ae){
...
...
}
}

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...