Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Thursday, 10 October 2013

Java Inner Classes - Example and Explanation

Inner Class in java so called nested class is nothing but a class that is being defined inside another class. Inner classes are beneficial in two scenarios i.e. access control and naming control. Although these benefits can be acquired using java packages.

Please note that we are not covering ‘Anonymous inner classes’ and ‘Static nested classes’ here. We are just talking about regular inner classes and their properties. ‘Anonymous inner classes’ and ‘Static nested classes’ are special case of inner classes that are covered in other tutorials.



How to create an inner class in Java

An normal inner class can be created in any class, with any type of access modifier (public, private, protected & default).
[modifier] class OuterClass {

 [modifier] class innerClass {
 }
}


Java Inner Class Properties

1) We can create a long hierarchy of inner classes as long as we want to :
private class OuterClass {
  public class InnerClassA {
   public class InnerClassB {
   }
  }
 }


2) Outer class can create as many numbers of instances of inner class inside its code.
package com.beingjavaguys.com;

public class OuterClass {

 class InnerClass {
  public void printMe() {
   System.out.println("I am inner class !");
  }
 }
 
 void callInner(){
  InnerClass inner = new InnerClass();
  inner.printMe();
  
  InnerClass inner1 = new InnerClass();
  inner1.printMe();
 }
}


3) The instance of a inner class in static method of outer class can be created in either of the two ways.
package com.beingjavaguys.com;

public class OuterClass {

 class InnerClass {
 }

 static void callInner() {
  /*
   * way of creating an inner class object in static method of outer class
   */
  OuterClass ouetClass = new OuterClass();
  OuterClass.InnerClass inner = ouetClass.new InnerClass();

  /*
   * another way of creating an inner class object in static method of
   * outer class
   */
  InnerClass inner1 = new OuterClass().new InnerClass();
 }
}


4) To create an instance of inner class in another class, other than the outer class we must have a instance of outer class.This can be done in either of the two ways.
package com.beingjavaguys.com;

public class OuterClass {

 class InnerClass {
 }

}

class AnotherClass {
 static void callInner() {
  /*
   * way of creating an inner class object in static method of outer class
   */
  OuterClass ouetClass = new OuterClass();
  OuterClass.InnerClass inner = ouetClass.new InnerClass();

  /*
   * another way of creating an inner class object in static method of
   * outer class
   */
  OuterClass.InnerClass inner1 = new OuterClass().new InnerClass();
 }
}


5) No inner class object is automatically instantiated with creation of outer class’s object.

6) An inner class have free access to all members of its outer class, no matter what the access level of outer class members has.
package com.beingjavaguys.com;

public class OuterClass {

 String def = "default";
 public String pub = "public";
 private String pri = "private";
 protected String pro = "protected";

 class InnerClass {
  void printMe() {
   System.out.println(def + " " + pub + " " + pri + " " + pro);
  }
 }

}


7) In case the inner class have same variable name as the outer class, than outer class variable can be called as follows:
package com.beingjavaguys.com;

public class OuterClass {

 public String pub = "Outer - public";

 class InnerClass {
  public String pub = "Inner - public";

  void printMe() {

   // i am calling local vaiable
   System.out.println(pub);

   // i am calling outer class variable
   System.out.println(OuterClass.this.pub);
  }
 }

}


8) Inner class members can be accessed by outer class with the instance of inner class, no matter what the access specifier they have. Outer class members can be accessed within inner classes directly, no matter what the access specifier they have.
package com.beingjavaguys.com;

public class OuterClass {

 String outerVar = "outerVar";

 void callInner() {
  /*
   * inner class members can be called in a outer class using inner
   * class's instance, no matter what the access specifier is
   */
  InnerClass innerClass = new InnerClass();
  System.out.println(innerClass.def + "" + innerClass.pub + ""
    + innerClass.pri + "" + innerClass.pro);
  innerClass.callOuter();

 }

 class InnerClass {
  String def = "default";
  public String pub = "public";
  private String pri = "private";
  protected String pro = "protected";

  void callOuter() {
   /*
    * outer class members can be called directly in a inner class, no
    * matter what the access specifier is
    */
   callInner();
   System.out.println(outerVar);
  }

 }

}



9) We can not have a static member in an inner class, not even ‘public static void main()’. If we does so, the inner class should be declared static, and this is not the case of inner class, static inner classes are special case and are called static nested classes.


Compilation and class file generation for inner classes.

As we have discussed an outer class can have a number of inner classes in it, each inner class will have a separate class file generated by compiler. Consider following example.
package com.beingjavaguys.com;

// Outer class
public class OuterClass {

 // Inner class2
 class Inner1 {
 }

 // Inner class2
 class Inner2 {
 }

 public static void main(String[] args) {

  // Anonymous inner class1
  new Object() {
  };

  // Anonymous inner class2
  new Object() {
  };

 }

}


In the code above we have an outer class, two normal inner classes and two anonymous inner classes. On compilation there will be five class files generated.

1) Outer.class
2) Outer $Inner1.class
3) Outer $Inner2.class
4) Outer$1.class
5) Outer$2.class


Here we are done with normal inner classes in java and their properties, in our upcoming blogs we will see ‘Anonymous inner class’ & ‘Static nested classes’ and more about java.








Thanks for reading !
Being Java Guys Team




2 comments:

  1. Good posting, java is most popular programming language now a days and being used a lot.

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft