Java Inner Class – Types of Inner Classes in Java

1. Objective

After Java Regular Expression, we move towards this Java Inner Class Tutorial. Here, we are going to learn what is Java inner class and types of inner classes in Java.

So, let’s begin Java Inner Class Example.

Java Inner Class - Types of Inner Classes in Java

Java Inner Class – Types of Inner Classes in Java

2. What is Java Inner Class

Inner class in Java is basically a class in a class, i.e. a member of another class. There are 4 types of inner classes in Java.

  1. Nested Inner class
  2. Method local Inner class
  3. Anonymous Inner class
  4. Static nested class

Do you know what is Abstract Class in Java?

3. Types of Inner class in Java

a. Nested Inner Class in Java

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

class Outer
      {
        // Simple nested inner class
           class Inner
                {
                  public void show()
                       {
                         System.out.println( “nested class method");
                       }
                 }
        }
class Main
      {
          public static void main(String[] args)
              {
                 Outer.Inner in = new Outer().new Inner();
                 in.show();
             }
       }

b. Method Local Java Inner class/ Java Local Class

A Method local inner class is a Java inner class defined within a method of an outer class in java.
Example 

class Outer
      {
          void outerMethod()
               {
                  System.out.println("inside outerMethod");
                  class Inner
                        {
                             void innerMethod()
                                  {
                                        System.out.println("inside innerMethod");
                                  }
                       }
                      Inner y = new Inner();
                      y.innerMethod();
                 }
        }
class MethodDemo
      {
           public static void main(String[] args)
             {
                   Outer x = new Outer();
                   x.outerMethod();
            }
     }

Let’s revise Java Character Class Method

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.

class Outer
     {
        void outerMethod()
              {
                  int x = 98;
                  System.out.println("inside outerMethod");
                  class Inner
                       {
                         void innerMethod()
                               {
                                    System.out.println("x= "+x);
                               }
                       }
                     Inner y = new Inner();
                     y.innerMethod();
                }
       }
class MethodLocalVariableDemo
         {
            public static void main(String[] args)
                    {
                        Outer x=new Outer();
                        x.outerMethod();
                    }
          }

Output 
local variable x is accessed from within inner class;
needs to be declared final

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

class Outer
      {
          void outerMethod()
             {
                final int x=98;
                System.out.println("inside outerMethod");
                class Inner
                      {
                           void innerMethod()
                                  {
                                     System.out.println("x = "+x);
                                  }
                      }
               Inner y = new Inner();
              y.innerMethod();
              }
       }
class MethodLocalVariableDemo
           {
                 public static void main(String[] args)
                           {
                                 Outer x = new Outer();
                                 x.outerMethod();
                             }
             }

Let’s explore What aisJava Datatypes

c. Static inner class

A static inner class in Java is a static member of an inner class, so technically they are not a Java inner class.
Example

class Outer
      {
        private static void outerMethod()
             {
                 System.out.println("inside outerMethod");
             }
       static class Inner
                     {
                         public static void main(String[] args)
                                 {
                                    System.out.println("inside inner class Method");
                                    outerMethod();
                                 }
                    }
       }

d. Anonymous Inner class

Anonymous Inner class in Java is an inner class that has no name, i.e. they are declared without any name. There are two ways to declare them.

i. As a subclass of a specified type

class Demo
  {
   	   void show()
    {
          	   System.out.println("demo");
  		 }
  }
class Flavor1Demo
  {
    	  static Demo d = new Demo()
    {
       		    void show()
      {
          		   super.show();
           		   System.out.println("demo");
     		       }
   		};
                      	public static void main(String[] args)
                         {
      		                   d.show();
                              }
  }

ii. As an implementer of the specified interface

class Flavor2Demo
  {
   static Hello h = new Hello()
 		{
       	            public void show()
              {
                                 System.out.println("i am in anonymous class");
        		  }
   		 };
        public static void main(String[] args)
    {
                     h.show();
   	        }
       }
                   interface Hello
                         {
                              void show();
                         }

4. Conclusion

In this tutorial for Java, we learned about the Java inner classes, types of inner classes in Java and their description with examples. This will help us understand and nest the classes and methods better and thus code better.
To learn Java operators and Java Access Modifiers follow these links.
For reference

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 *