Tuesday, 26 March 2013

Textual description of firstImageUrl

Inner classes in java

Inner class :

We can think of inner classes as part of outer class instance. Members of class can see each other, similarly inner class can see outer class member variables .
public class MainClass
{
      int x ;
      
 public void abc() 
 {
  
 }
 public static void testInnerClass()
 {
  // wrong. can not instantiate like this in a static method
  //InnerClass c = new InnerClass();
 }
      
      public static void main(String[] args) {
     //this is the way to instantiate Inner class from outside method
     MainClass.InnerClass innerClass = new MainClass().new InnerClass();
 }
      class InnerClass
      {
           MyOuter.this.abc();
           //static int a=0;  wrong !! can't have static fields 
      }

} 
Things to remember :
  • Inner classes are closely bound with running instance of outer class, so it can not have static declaration of any kind , and it can not access any static member of outer class
  • Always remember inner class are associated with instance only.
  • To get running instance of outer class(i.e. this of outer class )you say MyOuter.this
  • you must create instance of outer class first in order to create Inner class Instance


Method Local Inner class :

If a class is defined in a method body , it is called method local inner class
public class MethodLocalInnerClassTest{
 int x ; 
 public void methodContainingClass(int notFinalLocalvariable )
 {
  final String localFinalVariable = "abc";
  System.out.println("inside method");
  //MethodClass mclass  = new MethodClass(); can not instantiate before 
  class MethodClass
  {
   
   public void abc()
   {
      System.out.println("outer class var" + x);
      //System.out.println(notFinalLocalvariable); won't compile
      System.out.println(localFinalVariable); //compiles
   }
  }
  MethodClass obj = new MethodClass();
  
 }
}

Things to remember :
  • Method local inner classes can not have specifier like public , private etc , just like we can not have these specifiers for local variables in a method
  • Method local classes can access member variables of outer class .
  • Method local inner classes can not see and access local variables of method unless the variable is declared as final[because local variables scope is limited to method only]
  • Method class can be instantiated only from within the method
  • If the method is static , then it can only see static member variables of outer class

Anonymous Inner class :

Anonymous classes are those classes which does not need to have name.
public class BlogTest {
 int x ; 
 
 public void overrideMe()
 {
  System.out.println("normal method");
 }
 
 public static void main(String[]args)
 {
  BlogTest test = new BlogTest(){
   public void overrideMe()
   {
    System.out.println("overriden by anonymous class");
   }
   public void notOverRidden()
   {
    System.out.println("it is not overridden");
   }
   
  };
  test.overrideMe();
 //test.notOverRidden(); won't compile
 //implementing interface
 Runnable myRunnable = new Runnable()
  {
 public void run()
 {
  System.out.println("implementing interface");
 }
      };
  }
}

Here it means , declare and instantiate variable test of BlogTest type , create a new anonymous class(without a name) which extends BlogTest and override method in it. So instead of creating a new class then extending it , we have extended and overridden the method within the object creation itself . If the type is interface , then we are creating an anonymous class which implements the interface type . So in the same way we can have implementation in the same line.
Only methods which are part of type , can be called , you won't be able to call other methods.

Similarly we can create anonymous class while passing parameter to method .</br>
Thread class take argument of type runnable. We are creating a anonymous class implementing Runnable and passing it at the same time to the Thread.
Thread t = new Thread(new Runnable(){
 public void run()
 {
           System.out.println("running");
 }
 });

Static Nested Class :

These are classes which are nested inside the enclosing class and declared as static member variable of outer class. It means it's just another static member of the outer class.So these are not really inner classes but nested classes .it can not access variables of outer class like inner classes discussed above.
public class StaticNestedClassExample {
 
private int outerVar;
 
private static int staticVar;
  static class Nested
  {
     //outerVar = 420; can not access member variables 
     //staticVar = 5;// can not access any variable
  public static void print()
 {
    System.out.println("hello world!!");
 }
  }
 
  class OuterClass
 {
    public void testIt()
   {
      //this is how we instantiate static class from outside of the enclosing class
      StaticNestedClassExample.Nested nested = new StaticNestedClassExample.Nested();
    }
  }
 
   public static void main(String[] args)
   {
     //this is how you instantiate static nested class from inside of the enclosing class
     Nested n = new Nested(); 
   }
}


Post Comments and suggestions !!