Tuesday, 11 February 2014

Local Classes

Local classes are classes that are defined inside a block.

The statements in between '{' and '}' are called block of statements

Declaring Local Classes
Local classes can be declared inside any block like if, else, for etc.,

Local Class in a Method
class LocalClassEx{
 void printData(String s){
  /* Local Class */
  class Print{
   void printStars(){
    System.out.println("*****************");
   }

   void printHash(){
    System.out.println("##################");
   }

   void printDefault(){
    System.out.println("--------------------");
   }
  }

  Print p1 = new Print();
  
  if(s.equals("*")){
   p1.printStars();
  }
  else if(s.equals("#")){
   p1.printHash();
  }
  else{
   p1.printDefault();
  }
  
 } // End printData

 public static void main(String args[]){
  LocalClassEx obj = new LocalClassEx();
  obj.printData("*");
  obj.printData("#");
  obj.printData("abc");
 }
}
   
Output
*****************
##################
--------------------

Local class inside a block
class LocalClassEx{
 public static void main(String args[]){
  LocalClassEx obj = new LocalClassEx();
  String s = "*";

  {
   class Print{
    void printStars(){
     System.out.println("*****************");
    }

    void printHash(){
     System.out.println("##################");
    }

    void printDefault(){
     System.out.println("--------------------");
    }
   }

   Print p1 = new Print();

   if(s.equals("*")){
    p1.printStars();
   }
   else if(s.equals("#")){
    p1.printHash();
   }
   else{
    p1.printDefault();
   }
  }
 }
}

Output
*****************

1. Local class has access to the members of enclosing class.
class LocalClassEx{
 String name;

 String getName(){
  class MyName{
   String getName(){
    return name;
   }
  }

  MyName m1 = new MyName();
  return m1.getName();
 }

 void setName(String name){
  this.name = name;
 }

 public static void main(String args[]){
  LocalClassEx obj = new LocalClassEx();
  obj.setName("Joel");
  System.out.println(obj.getName());
 }
}

Output
Joel

2. Local class access the local variables only if those are declared as final
Example
class LocalClassEx{

 void localEx(){
  int var1 = 11;
  class Person{
   void printVar(){
    System.out.println(var1);
   }
  }

  Person p1 = new Person();
  p1.printVar();
 }

 public static void main(String args[]){
  LocalClassEx obj = new LocalClassEx();
  obj.localEx();
 }
}

In the above program, “var1” is a local variable to the method localEx(), Trying to access the local variable “var1” from local class causes the compile time error. when you try to run the above program, compiler throws the below error.
LocalClassEx.java:8: error: local variable var1 is accessed from within inner class; needs to be     declared final
System.out.println(var1);
^
1 error
If you update the local variable var1 as final, then Above program, compiles and runs fine.

3. Local class access the parameters of a method only if those are declared as final
Example
class LocalClassEx{
 void localEx(int var1){
  class Person{
   void printVar(){
    System.out.println(var1);
   }
  }
  Person p1 = new Person();
  p1.printVar();
 }

 public static void main(String args[]){
  LocalClassEx obj = new LocalClassEx();
  obj.localEx(10);
 }
}
  

In the above program, “var1” is a parameter to the method localEx(), Trying to access the parameter “var1” from local class causes the compile time error. when you try to run the above program, compiler throws the below error.

LocalClassEx.java:6: error: local variable var1 is accessed from within inner class; needs to be declared final
System.out.println(var1);
^
1 error

4. Local classes are a kind of inner classes, so defining static non final variables causes the compile time error
Example
class LocalClassEx{
 void localEx(int var1){
  class Person{
   static String name="HI";
  }
 }
}
       

When you try to compile the above program, compiler throws the below error

LocalClassEx.java:5: error: Illegal static declaration in inner class Person
static String name="HI";
^
modifier 'static' is only allowed in constant variable declarations
1 error
      

5. Local classes in static methods can only refer to static members of the enclosing class

6. Local classes are non-static because they have access to instance members of the enclosing block.

7. Local class can't contain static methods, Doing so causes the compile time error
        


Inner Class                                                 Anonymous Classes                                                 Home

No comments:

Post a Comment