Java Nested and Inner Classes

In Java, it is possible to define a class within another, such classes are known as nested classes.

The scope of a nested class is bounded through the scope of its enclosing class. Thus, if a class B is defined within a class A, then B doesn't exist independently of class A.

A nested class has access to the members, including private members, of the class in which it is nested. On the other hand, the enclosing class doesn't have access to the members of the nested class. A nested class which is declared directly in its enclosing class scope is a member of its enclosing class. It is also possible to declare a nested class which is local to a block.

There are two types of nested classes given here :

Java Static Nested Class

A static nested class is the one which has the static modifier applied. As it is static, so it must access the non-static members of its enclosing class by an object i.e., it can't refer to non-static member of its enclosing class directly. As of this restriction, the static nested classes are rarely used.

Java Non-Static Nested Class - Inner Class

The most crucial types of nested class is the inner class. And an inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and might refer to them directly in same way that the other non-static members of the outer class do.

Here this example program illustrates how to define and use an inner class. The class Outer has one instance variable named outer_x, and one instance method named test(), and defines one inner class called Inner.

/* Java Program Example - Java Nested and Inner Classes
 * This program demonstrates an inner class in Java  */
 
 class Outer
 {
     int outer_x = 100;
     
     void test()
     {
         Inner inner = new Inner();
         inner.display();
     }
     
     /* this is an inner class */
     class Inner
     {
         void display()
         {
             System.out.println("display : outer_x = " + outer_x);
         }
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
    
         Outer outer = new Outer();
         outer.test();
       
     }
 }

When the above Java program is compile and executed, it will produce the following output:

java nested and inner classes

In the previous program, an inner class named Inner is defined within the scope of the class Outer. Therefore, any code in the class Inner can directly access the variable outer_x. An instance method named display() is defined inside Inner. And this display() method displays outer_x on the standard output stream. The main() method of JavaProgram creates an instance of the class Outer and invokes its method named test(). That method creates an instance of class Inner and the display() method is called.

It is important to understand that an instance of Inner can be created only in the context of the class Outer. The Java compiler produces an error message otherwise. In general, an inner class instance is often created by the code within its enclosing scope, as the example does.

As explained, an inner class has access to all of the members from its enclosing class, but the reverse is untrue. Members of the inner class are known only within the scope of the inner class and may not be used through the outer class. For instance, consider the following example program :

Java Nested and Inner Classes Example

Here is an example program in Java, uses nested and inner class in Java:

/* Java Program Example - Java Nested and Inner Classes.
 * This program produce an error message.
 * For instance, in BlueJ, it will produce 
 * cannot find symbol - variable y
 * So this program will not compile. 
 */
 
 class Outer
 {
     int outer_x = 100;
     
     void test()
     {
         Inner inner = new Inner();
         inner.display();
     }
     
     /* this is an inner class */
     class Inner
     {
         int y = 10;     // y is local to Inner
         
         void display()
         {
             System.out.println("display : outer_x = " + outer_x);
         }
     }
     
     void showy()
     {
         System.out.println(y);      // error!, y not known here !!
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
         
         Outer outer = new Outer();
         outer.test();
         
     }
 }

Here, y is declared as an instance variable of Inner. Thus, it isn't known outside of that class and it can't be used by showy().

Java Online Test


« Previous Tutorial Next Tutorial »