Inner/Nested Class – Method Local-Inner Class Example

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.

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.

Output:
op1

Leave a Reply

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