S

 

Inner Classes

Inner classes let you define one class within another.

They provide a type of scoping for your classes since you can make one class a member of another class.

Just as classes have member variables and methods, a class can also have member classes.

class Outer {
  /class Inner { }
}// End of class

if you compile it,
%javac Outer.java

you’ll end up with two class files:
Outer.class
Outer$Inner.class


e.g.

class Outer {
  private int x = 5;

public void outerMethod() {
Inner in = new Inner();
in.innerMethod();
}

class Inner {
public void innerMethod() {
System.out.println("Outer variable x is " + x);
}
}
}// End of class


You can create four different types of inner classes, based upon the how and the where of creation.
  Nested top-level classes
If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. Top-level inner classes implicitly have access only to static variables. There can also be inner interfaces. All of these are of the nested top-level variety.

Member classes
Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class.

Local classes
Local classes are like local variables, specific to a block of code.Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.

Anonymous classes
Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.
 


[ Received from Seeny Desai ]

I
Idempotent
inheriance
instance method
instance variable
interface
inner classes
instanceof
iterator
Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions