4 Types of Java Inner Class – You Must Know!

What is Java Inner Class?

Before understanding the term inner class you should know about what are java classes. Basically inner class is the class inside a class (or a member of another class). In other words, we can say that there is a parent class and inside that, there is a child class, which is also called Inner Class in Java.

Types of Inner Class in Java

There are four types of inner classes-

Types of Inner Class in Java

1. Nested Inner Class

This class can access any private instance variable in java which can be of any outer class. We can also define the access of this class, i.e. private, public or protected.

package JavaInnerClass;
  class JavaOuterClass 
    {
       // private variable of the outer class
       private int value = 60;  
       // inner class
  class JavaInnerClass 
   {
         // public variable of the inner class
        public int getValue() 
        {
           System.out.println("This is the getValue method of inner class:");
           return value;
         }
    }
    }
public class JavaNestedClass
{  
  public static void main(String args[]) {
          // Instantiating the outer class
          JavaOuterClass outer = new JavaOuterClass();
          
          // Instantiating the inner class
          JavaOuterClass.JavaInnerClass inner = outer.new JavaInnerClass();
          System.out.println(inner.getValue());
       }
    }

Output-nestedclass

2. Method Local Inner Class

Here we can write a class inside a method and this will be a local type. Like local variables, the scope of the inner class is limited within the method. A local inner class can be initialized only inside the method where the inner class is defined.

Learn datatype in Java with Examples

The following program shows how to use a method-local inner class.

Example 

package JavaInnerClass;
class Outer
  {
    void outerMethod()
     {
        System.out.println("inside outer method");
         class Inner
         {
           void innerMethod()
            {
              System.out.println("inside inner method");
             }
          }
         Inner innerObj = new Inner();
         innerObj.innerMethod();
      }
  }
public class MethodLocalClass {
   public static void main(String[] args)
           {
             Outer outerObj = new Outer();
             outerObj.outerMethod();
           }
   }

Output-methodlocalclass

Method Local class cannot access a local variable from the outer class until it is defined as final. In the example below, the compiler generates an error.

But below code doesn’t generate any error, let us have a look into it

class Outer
{
   void outerMethod()
      {
        final int var1=98;
        System.out.println("inside outer method");
        class Inner
        {
          void innerMethod()
          {
           System.out.println("Value of var1 is = "+var1);
          }
       }
        Inner innerObj = new Inner();
        innerObj.innerMethod();
      }
 }
class MethodLocalClass1
     {
       public static void main(String[] args)
        {
         Outer outerObj = new Outer();
         outerObj.outerMethod();
        }
       }

Output-

methodLocalClass1

Do you know what is java.io.File Class?

3. Static Inner Class

It is a static member of an outer class. We can access it without initializing the outer class, using a static method. So, technically they are not a Java inner class. Similar to static members, a static nested class does not have access to the instance variables and the methods of the outer class.

Example-

public class staticInnerClass {
    static class nestedDemo 
    {
	   public void myMethod()
	   {
		   System.out.println("This is a nested class");
		}
	    public static void main(String args[]) {
		      staticInnerClass.nestedDemo nested = new staticInnerClass.nestedDemo();	 
		      nested.myMethod();
		   }
		}
}

Output-static Inner Class

4. Anonymous

Anonymous inner class is a class in Java that has no name, i.e. they are declared without any nameThese type of classes help you to make your code more concise. It enables you to declare and initialize a class at the same time. They are similar to local classes except that they do not have a name. We can use them if you need to use a local class only once.

Recommended Reading – Abstract Class in Java

Example-

interface anonymousAnimal
  {
  void type();
   }

public class anonymousInnerClass {
   public static void main(String args[])
   { 
     
     anonymousAnimal animal = new anonymousAnimal(){         
    public void type()
    {
     System.out.println("Annonymous Anteater");
     System.out.println("Annonymous Unicorn");
     System.out.println("Annonymous Capybara");
     System.out.println("Annonymous Beaver");
    }
   };
   animal.type();
   }
  }

Output- Anonymous Inner Class in Java

Summary

By the end of this tutorial, we can say Java inner class is the class declared inside another class, which is different from a subclass. A programmer can use this class in four ways to accomplish the task. Now, it’s your time to practice these classes.

Step up your Career in Java and grab your dream job now!

If you face any issues, please share with us. Our experts will try to resolve your issues.

1 Response

  1. zack says:

    Note!!!
    Correct this section please or at-least mention the java version which you are using.- “Method Local class cannot access a local variable from outer class until it is defined as final. In the example below, the compiler generates an error.”
    From Java 8 all the method local variable which we are using inside method local inner class is bydefault final. So final keyword is not mandatory.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.