Java Inner Class



Inner classes are essentially classes defined inside other classes and act like members of the enclosing class. In this lab, you are going to learn basic concept of inner class.




Exercises and homework


Exercise 1: Build and run Java applications that use inner class


(1.1) Build and run a simple application that uses an inner class


0. Start the NetBeans IDE if you have not done so.
1. Create a NetBeans project



Figure-1.10: Create a new NetBeans project
2. Modify the IDE generated Main.java as shown in Code-1.11 below.

package innerclassexample;

public class Main {
   
    public static void main(String[] args) {
       
        // Create an object instance of a class that contains
        // an inner class - we will call it outer class.
        OuterClass oc = new OuterClass();
       
        // Create an object instance of an inner class.
        OuterClass.InnerClass ic = oc.new InnerClass();
       
        // Display data from both outer class and inner class.
        System.out.println("Access data from outer class = " + oc.data);
        System.out.println("Access data2 from inner class = " + ic.data2);
       
        // Invoke a method from an inner class
        ic.method();
    }
   
}
Code-1.11: Main.java

3. Write OuterClass.java as shown in Code-1.12 below.

package innerclassdemo;

/**
 *
 * @author sang
 */
public class OuterClass {
   
    /** Creates a new instance of OuterClass */
    public OuterClass() {
    }
   
    // Define a variable in the outer class
    int data = 5;

    // Define an inner class
    class InnerClass {
        int data2 = 10;
        void method() {
            System.out.println("data from OuterClass = " + data);
            System.out.println("data2 from InnerClass = " + data2);
        }
    }
    
}
Code-1.12: OuterClass.java

3. Build and run the program
Access data from outer class = 5
Access data2 from inner class = 10
data from OuterClass = 5
data2 from InnerClass = 10
Figure-1.13: Result



Summary

In this exercise, you have build and run Java applications that use an inner class.
   
                                                                                                                                   Return to the top



Homework


1. The homework is to modify the InnerClassExample project as following.  (You might want to create a new project by copying the InnerClassExample project.  You can name the homework project in any way you want but here I am going to call it MyOwnInnerClassExample.)

package innerclassexample;


public class InnerClassDemo {
   
    public static void main(String[] args) {
       
        // Create an object instance of a class that contains
        // an inner class - we will call it outer class.
        OuterClass oc = new OuterClass();
       
        // Create an object instance of an inner class.
        OuterClass.InnerClass ic = oc.new InnerClass();
       
        // Create an object instance of an inner class.
        OuterClass.InnerClass.InnerInnerClass iic = ic.new InnerInnerClass();       
       
        // Display data from both outer class and inner class.
        System.out.println("Access data from outer class = " + oc.data);
        System.out.println("Access data2 from inner class = " + ic.data2);
        System.out.println("Access data3 from innerinner class = " + iic.data3);
       
        // Invoke a method from an inner class
        ic.method();
        iic.method2();
    }
   
}
Code-1.15: Modified InnerClassDemo.java
Access data from outer class = 5
Access data2 from inner class = 10
Access data3 from innerinner class = 15
data from OuterClass = 5
data2 from InnerClass = 10
data from OuterClass = 5
data2 from InnerClass = 10
data3 from InnerInnerClass = 15
Figure-1.16: Output