Java Inner Class

Java Inner class questions are asked to put us in confusion and it is always better to know type of inner classes and how they can access the outer class details. Reasons for using inner classes, among them:
  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.
Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.



Static inner class
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.


Instance inner class

A non-static inner class has the outer class as an instance variable, which means it can only be instantiated from such an instance of the outer class:

Anonymous inner class
An anonymous inner class can come useful when making an instance of an object which certain "extras" such as overloading methods, without having to actually subclass a class. In below example we are using Thread using anonymous inner class.

  1. Anonymous class is declared and initialized simultaneously.
  2. Anonymous class must extend or implement to one and only one class or interface resp.
  3. As anonymouse class has no name, it can be used only once.
Below example explains mainly we have 3 type of inner classes.


package com.learning.basic;
public class InnerClassLearning {
public static String data = "static";
public String instanceVariable = "instance";
// Static inner Class
private static class TestStaticInner {
public static void validate() {
System.out.println(" -- static inner class -- ");
String innerData = data;
System.out.println(" Static inner can access outer class static member");
// String data = instanceVariable; // Not possbile
}
}
// Instance inner class
class TestInner {
public void validate() {
System.out.println(" -- instance inner class -- ");
String innerData = data;
System.out.println(" Instance inner can access outer class static member");
String data = instanceVariable;
System.out.println(" Instance inner can access outer class instance member");
}
}
// anonymous inner class
public void validate() {
Thread a = new Thread (new Runnable() {
@Override
public void run() {
System.out.println(" -- Anonymous inner class -- ");
System.out.println(" Anonymous inner class instance");
}
});
a.start();
}
public static void main(String... strings) {
InnerClassLearning.TestStaticInner.validate();
InnerClassLearning outer = new InnerClassLearning();
// Calling anonymous inner class
outer.validate();
// Syntax for creating the inner class instance
InnerClassLearning.TestInner testInner = outer.new TestInner();
testInner.validate();
}
}


4 comments:

  1. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.

    Tam
    www.gofastek.com

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic java inner class concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete