Inner Classes In Java – Static, Non Static, Method Inner and Anonymous Classes

Inner Classes In Java – Static, Non Static, Method Inner and Anonymous Classes

What are inner classes?
A class which is defined within another class is the inner class.

What is the use of inner class?
The  inner class is mainly used as logically grouping classes at one place.If you have a class and that class is used only by one other class, then we can group the first class within another class.

There are two types of Inner class,

  • Static Nested Class
  • Non Static Nested Class
    • Method Local
    • Anonymous Inner Class

Static Nested vs Non-Static Nested Class:
Static Nested Class cannot access outer class variables and methods, where as Non Static nested class have full access to outer class variables and methods.


Static Nested Class:

Static class declared within the outer class.

To access the static inner class,
outerclass.innerclass innerclass = new outerclass.innerclass()

Output:

Non Static Nested Class:

Non Static class declared within the outer class.

outerclass.innerclass innerclass = new outerclass.innerclass()

Output:

Method Local Inner Class:

A class declared within a method and create object and call method within the same method.

Output:

Anonymous Nested Class:

In Anonymous inner class, we use the interface as nested class.

Interface cannot be initialized, so how are we using new keyword?

If we look through the code, we use new keyword for interface but we are not actually creating an object. We use new and implement the interface method and close the curly braces with colon.

Output:

Read More

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

Read More