Inner Classes in JAVA (static, local and anonymous)


Inner class (Nested Classes) : as the name sounds a class which is define inside a class .

syntax for creating inner class :

[modifiers] class OuterClassName {
code…
[modifiers] class InnerClassName {
code….
}
}

Some key points about Inner class :

A) 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.

B) Inner classes are actually a phenomenon of the compiler and not the JVM.

C) 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.

D) No inner class objects are automatically instantiated with an outer class object.

E) 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.

F) Inner class code has free access to all elements of the outer class object that contains it, by name, if the inner class has a varible with same name then the outer class’s variable can be accesse like this:

<OuterClassName>.this.<variableName>

Inner classes may be defined with following access modifiers : public, protected, private, or with default package access.
Based on these modifiers our next category of inner class is :

Static Inner Class

syntax :

class OuterClassName {
public static class {
. . .
}
. . .
}
Difference b/w static and non static inner class :

A) Static members of the outer class are visible to the static inner class, what ever their access level be but Non-static members of the outer class are not available, because there is not instance of the outer class.
B) 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.

Local Inner Classes:

Nested class which is define inside the body of a method of outer class is known as local inner classs.

Syntax :

 

<access-specifier> class <OuterClassName> {

code…

<access-specifier> <return-type> <MethodName>(<arguments>){

class <LocalInnerClassName>{
code…
}
code…
}
code…
}
Since local inner classes are define inside a method scope of these classes are always restricted to the block in which they are declared. They are completely hidden from the outside world.

Anonymous Inner Classes
Local inner class which is define without name .

Syntax :

new SuperType(construction parameters) {
inner class methods and data
}

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.

Summary of all above Inner classes and compilation :

// Main class
public class Main {

// Inner class Test1
class Test1 {
}

// Inner class Test2
class Test2 {
}

public static void main(String [] args) {

// Anonymous inner class 1
new Object() {
};

// Anonymous inner class 2
new Object() {
};

System.out.println(“Hello World”);
}
}

After Compilation by JVM

Main.class
Main$Test1.class
Main$Test2.class
Main$1.class
Main$2.class

Diffrent .class files are generated.

 

Author: Pooja Arora