Learn inner classes in Java


Inner class, also called Nested class, is a non-static class which is defined inside another class. Inner classes are nothing but classes that are defined within other classes. The nesting is a relationship between classes, not objects.
They may be defined as public, protected, private, or with package access. They may only be used "in the context" of the containing class (outer class, or enclosing class), unless they are marked as static

Java inner classes have feature that makes them richer and more useful. An object of an inner class has an implicit reference to the outer class object that instantiated it. Through this pointer, it gains access to any variable of the outer object. Only static inner classes don’t have this pointer. It is actually invisible when we write the code, but compiler takes care of it. Inner classes are actually a phenomenon of the compiler and not the JVM.

Inner Classes:


  • The outer class (the class containing the inner class) can instantiate as many number of inner class objects as it wishes, inside it’s code.
  • If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.
  • No inner class objects are automatically instantiated with an outer class object.
  • If the inner class is static, then static inner class can be instantiated without an outer class instance, otherwise, the inner class object must be associated with an instance of the outer class.
  • Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is), if the inner class has a varible with same name then the outer class’s variable can be accessed.
There are two more types of inner classes, i.e local inner classes & anonymous inner classes. The local inner class are defined within a method. Anonymous inner classes are also defined with in a method but have no name.


Local Inner Classes:


  • Local classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.
  • Local classes have a great advantage: they are completely hidden from the outside world.
  • They can not only access the instance variables but local variables of the method (in which they are defined) as well, but the local varible has to be declared final.

Anonymous Inner Classes:

When using local inner classes, you can often go a step further. If you want to make only a single object of this class, you don’t even need to give the class a name.
Such a class is called an anonymous inner class. Usually the inner class extend some interface or extend other class.



  • Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.
  • An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor. In particular, whenever an inner class implements an interface, it cannot have any construction parameters.

  • It is recommended to refrain from using them as many programmers find too many anonymous classes hard to read.

Static Inner Classes:


  • Static members of the outer class are visible to the static inner class, what ever their access level be.
  • Non-static members of the outer class are not available, because there is not instance of the outer class.
  • An inner class may not have static members unless the inner class is itself marked as static.
  • Sometimes static nested class are not refered to as inner class at all, as they don’t require outer classes instance.
  • A static inner class is just like any other inner class, but it dose not have the reference to its outer class object that generated it.
  • The outer class can call even the private methods of the inner class.

Example for Inner Classes:

public class OuterClass {
 private int x;
 OuterClass(int x, int y) {
  this.x = x;
  new InnerClass(y).privateDisplay();
 }
 public class InnerClass {
  private int y;
  InnerClass(int y) {
   this.y = y;
  }
  private void privateDisplay() {
   System.out.println("privateDisplay x = " + x + " and y = " + y);
  }
  public void publicDisplay() {
   System.out.println("publicDisplay x = " + x + " and y = " + y);
  }
 }
}

Explanation of above example:
  • OuterClass has one property, x; the inner class InnerClass has one property, y
  • the OuterClass constructor accepts two parameters; the first is used to populate x
  • it creates one InnerClass object, whose y property is populated with the second parameter
  • note that the inner class has free access to the private outer class x element
  • and the outer class has free access to the private inner class privateDisplay() method
The connection between the two classes is handled automatically

Reactions:

0 comments:

Post a Comment