Inner/Nested Class – Method Local-Inner Class Example
Java Inner Class
What is an Inner Class?
Inner classes are also known as Nested class, is a class which is declared within another class.

Advantages of Inner Class:

  • Inner class can access all the variables and methods of enclosing class including private members
  • Useful for Interface Implementations used by enclosing class
  • Fine control over interface implementation

Types of Inner Classes:
There are 4 types of Inner Classes,

  • Static Nested Class
  • Member Inner Class
  • Method Local-Inner Class
  • Anonymous Inner Class

Static Nested Class:
A class within another class is known as Static Nested Class

Member Inner Class:
Class created within a class but outside methods

Method Local-Inner Class:
Class created within a methods

Anonymous Inner Class:
Class created for implementing interface or extending class

Today, We are going to see an example for Method Local-Inner Class i.e. A class created inside a method of another class.

public class InnerExample {
          public void display()
            class methodinner
                public void displayMethodInner()
                System.out.println("Accessing Class inside Method");
            methodinner minner = new methodinner();
    public static void main(String args[])
       InnerExample innerexample = new InnerExample();

In the above example, We have created a class object for methodinner class and called displayMethodInner method within display method. This is called in order access the displayMethodInner method of methodinner class.

In Main block, We are calling the display method to print the method of methodinner class.


By Sri

Leave a Reply

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