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 are a 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.

How to Instantiate an Inner Class – Nested Class in Java

  1. Types of Nested Classes
    • Non Static
    • Static
  2. Access Modifiers for Nested or Inner Classes
  3. Features of Nested Class
  4. Advantages
  5. Sample code, explanation of code & output

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, including private fields and methods.

Syntax

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

Creation

<OuterClassName> outerObj = new <OuterClassName>(arguments);
<OuterClassName>.<InnerClassName> innerObj = outerObj.new <InnerClassName> (arguments);

Properties

  1. The outer class (the class containing the inner class) can instantiate as many numbers of inner class objects as it wishes, inside its code.
  2. 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.
  3. No inner class objects are automatically instantiated with an outer class object.
  4. The 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 the same name then the outer class’s variable can be accesse like this:
    <OuterClassName>.this.<variableName>
  5. The outer class can call even the private methods of the inner class.
  6. The inner class object must be associated with an instance of the outer class.

Static Nested Class

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

Syntax 

<access-specifier> class OuterClassName {
  public static class <StaticInnerClassName> {
      code. . .
  }
  code . . .
}

Creation

<OuterClassName>.<InnerClassName> innerObj = new <OuterClassName>.<InnerClassName>(arguments);

Properties

  1. For static inner class, then the static inner class can be instantiated without an outer class instance.
  2. Static members of the outer class are visible to the static inner class, what ever their access level is.
  3. Non-static members of the outer class are not available, because there is no instance of the outer class.
  4. An inner class may not have static members unless the inner class is itself marked as static.
  5. Sometimes static nested classes are not referred to as inner class at all, as they don’t require outer class’s instance.
  6. A static inner class is just like any other inner class, but it does 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 the 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 of Nested Class

  • 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 as “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  private, but B can access them. Also, B itself can be hidden from the outside world.

Program 1

/* within the scope of outer class */
class Outer{
  int var1 = 2;
  int var2 = 3;

  /* Inner Class */
  class Inner{
    void add(){
      System.out.println("Addition is:"+(var1+var2));
    }
  }

  void show(){
    Inner in = new Inner();
    in.add();
  }
}

class NestedClassDemo{
  public static void main(String args[]){
    Outer out = new Outer();
    out.show();
  }
}

Output 1

code for nested class in java

Program 2

/* Outside the scope of outer class */
class Outer{
  int var1 = 2;
  int var2 = 3;

  /* Inner Class */
  class Inner{
    void add(){
      System.out.println("Addition is:"+(var1+var2));
    }
  }
}

class NestedClassDemo1{
  public static void main(String args[]){
    Outer.Inner in = new Outer().new Inner();
    in.add();
  }
}

Output 2

code for nested class in java

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 the outer class. We will specially instantiate inner classes within the scope of the outer class. The inner class is Private as we know private members will not be accessible 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. The syntax seems to be difficult, but it’s 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 the outer class. Then we will create an object for the Inner class as Inner class is the member of Outer class, so we have to recognize the Inner class uniquely, can be done with Outer. Inner then we have to create the object which is done by a new operator followed by the constructor of that particular class. The same thing is done here.

Flash on

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

Checkout more useful tutorials and definitive guidelines on Java programming.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

1 Comment

Click here to post a comment