Inner classes Java

Inner classes Java

Introduction to Inner classes Java

JDK 1.1 introduced a new type of classes known as inner classes. All the classes you have done so far are known as top-level classes. A class written inside another class is known as "inner class" or "nested class". That is, an inner class is enclosed within another class. Inner classes give more accessibility and scope restrictions for other classes.

The class which encloses is known as outer class and getting enclosed is known as inner class. Outer class is known as top-level class. The classes of a package are all top-level classes.

Types of Inner Classes Java

There are four styles of creating an inner class.

  1. Static member classes
  2. Member classes
  3. Local classes
  4. Anonymous classes

1. Static Member Classes

Here, the inner class is declared as static. Static inner classes are less used. Following program illustrates.

Inner classes Java

The inner class SecondOne is declared as static member of outer class FirstOne and includes a static method area(). When we compile the above program we get two .class files as follows.

  1. FirstOne.class
  2. FirstOne$SecondOne.class

The .class file of SecondOne inner class is placed within the outer class, FirstOne. The other classes cannot access directly the SecondOne and should go through FirstOne.


As the SecondOne is static, it is called with FirstOne directly. The SecondOne scope is within the FirstOne.

2. Member Classes

Here, the inner class is not declared as static. It is declared within the body of the outer class (not inside the method of outer class). Following are the scope rules.

  1. The outer class members (variables) can be accessed by inner class.
  2. The inner class members cannot be accessed by outer class.
  3. Inner class members can be accessed by inner class only.

Following program illustrates the above scope rules.

Inner classes Java

In the above code, First is outer class and Second is inner class. price is a variable of outer class and rate is a variable of inner class. By the scope rules, Second can access price but First cannot access rate .

When the above code is compiled, two .class are obtained as follows.

  1. First.class
  2. First$Second.class

As you can observe, the inner class, Second exists within the context of outer class, First.

First.Second fs = new First( ).new Second();

The Second class object cannot be created directly and must be done through the outer class, First.

The above statement can be modified as follows.

First.Second fs = Second();

The outer class object is created with the help of inner class object. Finally, inner class can access all the static and non-static fields of outer class (including private fields) and the inner class object cannot be instantiated directly and should be done through outer class object.

3. Local Classes

A local class is simply an inner class but declared within the method of outer class (earlier, the inner class exists directly in the body of the outer class but now exists inside a method). Here the rules of accessibility are, the inner class can access, anyhow, its own members and only the final parameters of the enclosing method .

Following program illustrates.

Inner classes Java

In the above code, the perimeter() method of inner class Son can access the final parameters of area(), the method of outer class Father. That is, the perimeter() method can access the final variables y and m of area(). But, the inner class can access the price, non-final variable of outer class as it is a field declared in the body of the outer class and not within the method.

When we compile the above program, we get the following classes.

  1. Father.class
  2. Father$1Son.class

Observe, it is $1 and not $.

4, Anonymous Inner Class

An anonymous inner class, as the name indicates, does not have a name. As it does not contain a name, it must be instantiated at the time of defining it. This type of anonymous classes is mostly used in event handling mechanism of AWT. Followng snippet of code is used to close the frame window.

In the above code, an anonymous inner class of WindowAdapter is used. The advantage of anonymous inner classes is better readability as all the code exists at the time of declaration only, all at one place. The disadvantage is the code cannot be used elsewhere (no reusability).

Advantages – Inner Classes

Following list gives the advantages of inner classes.

  1. Better accessibility restrictions. The inner classes are controlled through outer class instances.
  2. The life of inner class is controlled by outer class. Inner classes can use
    composition but through outer class.
  3. We can make a logical group of classes. If a class is useful particularly to only one another class, the class can be declared as inner class.
  4. Encapsulation is better implemented. The inner class members are hidden from other classes.
  5. The maintainability is easier as the code is closer to other class.

22 thoughts on “Inner classes Java

  1. Spoorthy

    Hello Sir, I have tried accessing the variable that is not final from the local class, it didnt show any error. i got the Output for the following program as
    10 20 30 40
    Could you please look into it

    package firstprogram;
    public class LocalClass
    int a = 10;
    final int b = 20;
    static int c = 30;
    private int d = 40;
    void methodOuter(int x,final int y)
    int e = 50;
    final int f = 60;
    //static int g = 70; local variables cant be static
    //private int h = 80; local variables cant be private

    class Inner
    int i = 90;
    final int j = 100;
    //static int k = 110;
    private int l = 120;

    void methodInner()
    System.out.println(a+” “+b+” “+c+” “+d);
    Inner i1 = new Inner();
    public static void main(String args[])
    LocalClass l1 = new LocalClass();

  2. Sunita Bansal .

    My question is that what is the difference between private inner class and non- private inner class. If I can access private and nonprivate class in the same way. for example in the below code line number 11. If I remove private from line number it also work same way.
    1. class outer {
    2. private class inner
    3. {int a=10;
    4. private void show()
    5. {System.out.println(“a=” + a);}
    6. } // end of inner class
    7. public static void main(String args[])
    8. {
    9. System.out.print(“Enter number”);
    10. outer obj = new outer();
    11. inner().show();
    12. } }

  3. Mann Chauhan

    Hello sir, your saying that the inner class can access, anyhow, its own members and only the final parameters of the enclosing method, but there is no error in this program. Run urself.

    package innerClasses;

    public class Father {
    double price = 10.5;

    public void area(int x, final int y){
    int k = x+y;
    final int m = x*y;

    class Son{
    public void perimeter(){

    System.out.println(“x is “+x);
    System.out.println(“y is “+y);
    System.out.println(“m is “+m);
    System.out.println(“k is “+k);
    System.out.println(“price is “+price);
    Son s1 = new Son();
    public static void main(String[] args) {
    Father f1 = new Father();
    f1.area(10, 20);


    1. S. Nageswara Rao, Corporate Trainer

      The outer class has got some purpose with the variable values. If the inner class changes them, it is loss to the outer class. For example, the outer class has a “double $rate = 61.55;”. If the inner class changes the $rate, then how the outer class will have data integrity. For this reason, the inner class is allowed to access final variables only.

  4. madhusudhan

    public class Testis {
    public static void main(String[] args) {
    new OuterClass(); // line 3
    class OuterClass {
    private int x = 9; // line 5
    public OuterClass() {
    InnerClass inner = new InnerClass(); // line 7
    class InnerClass {
    public void innerMethod() {

    note: Iam getting output 9
    but coming to the line 7..the way of instantiating the inner class
    OuterClass.InnerClass inner = new InnerClass();
    but in program it is
    InnerClass inner = new InnerClass();
    how is it possible

  5. madhusudhan

    public class outer
    public static void main(String[] a)
    static class inner{

    //Iam getting compiler error…we can declare inner class as Static..but why iam getting error..

    1. santu

      here ,class inner becomes local inner class and Local classes are similar to inner classes because they cannot define or declare any static members. … Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

    1. Saroj Kumar

      There is nothing like “Static member class”, rather “static class” or “member class”.
      We cannot keep main method in nested class, or more precisely non-static inner class, because main method being static is not allowed to be kept inside non-static inner class. In order to use main method in inner class, you will need to make inner class as static also.

Leave a Reply

Your email address will not be published. Required fields are marked *