You are here:

How to Instantiate an Inner Class – Nested Class in Java

In Java programming language, when a class is defined within another class then such a class is called a nested class or inner class. Nested classes is a very unique feature of Java that has been included since jdk1.1. Always remember, this nesting functionality is a relationship between classes only, not between Java objects. Previously we have talked about how to instantiate instance variable in Java, and now we will take a look at how to instantiate an inner class in Java.

Types of Nested Classes in Java

Non-static Nested Class

Non-static nested classes are called inner classes. It has access to all of its enclosing class’s instance data,

Creation

Properties

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.

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 variable with same name then the outer class’s variable can be accesse like this:
<OuterClassName>.this.<variableName>

The outer class can call even the private methods of the inner class.

The inner class object must be associated with an instance of the outer class.

Static Nested Class

Nested classes that are declared static are simply called static nested classes. A static class has no access to instance-specific data.

Creation

Properties

For static inner class, then static inner class can be instantiated without an outer class instance.

Static members of the outer class are visible to the static inner class, what ever their access level is.

Non-static members of the outer class are not available, because there is no 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 classes are not referred to as inner class at all, as they don’t require outer class’s 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.

Access Modifiers for Nested or Inner Classes

public

protected

private

default

Learn more about Java access specifiers/modifiers here.

Features of Nested Class

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 but can access all the static members only using outer class name. 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. This feature makes Java inner classes richer and more useful.

Advantages

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.

Output 2

Explanation of Code & Output

In the first program we have instantiated the nested class Inner in a method declared within the Outer class i.e.

within the scope of outer class. We will specially instantiate inner classes within the scope of outer class when it the inner class is Private as we know private members can’t be accessed outside the scope of that class containing private members.

In the second program we have instantiated the nested class Inner outside the scope of Outer class. Syntax seems to be difficult but its not like that. It’s too easy:

We will break this statement into two:

Outer out = new Outer();
Outer.Inner in = out.new Inner();

First we have to create an object for outer class. Then we will create object for Inner class as Inner class is the member of Outer class so we have to uniquely recognize the Inner class, can be done with Outer.Inner then we have to create the object which is done by new operator followed by the constructor of that particular class. Same thing is done here.

Flash on

After compiling nested class, two .class files will be generated with Outer.class and Outer$Inner.class (here class Inner is inside class Outer).