Inner Classes

Java Inner classes or also called as non-static nested class is a class that is declared inside a class or interface. Inner class is a security mechanism, as we know that we can’t define a class as private but an inner can be declared as private and thus it can access the private members of the class.

Now coming to the advantages of the nested or inner classes, there are 3 advantages of nested classes as follows:-

  • It can access all the variables and methods of the outer class including those which are defined as private.
  • It is used to logically group classes and interface in one place only hence it is used to develop more readable and maintainable code.
  • It helps in code optimization.

There are 3 types of inner classes:-

  1. Local Inner class
  2. Method local Inner class
  3. Anonymous Inner Class


Local Inner class or Inner class is the most basic inner class, where a class is written inside a class. Unlike outer class the inner class can have the private access modifier hence it can access all the members of outer class including those which are private. As the inner class is defined as private it cannot be accessed from an object outside the class.

Now let’s see a small example:

class Example1 {

//private variable of the outer class

private int sam= 11;

//inner class

public class Inner1{

public int getSam(){

System.out.println("This is the getsam method of the inner class");

return sam;




public class outer2{

public static void main(String args[]){

//Instantiating the outer class1

Example1 outer=new Example1();

//Instantiating the inner class

Example1.Inner1 Inner1();




In the above example, Example1 is the outer class and Inner1 is the inner class. The above example also shows how we can access a private member of a class using inner class.


Method local inner class is the second type of non-static inner class where we write a class inside a method. As a method local variable the life or scope of the method local class is within the method only and we can instantiate the class within the method only where it is defined.


public class Example2{

//instance method of the outer class

Public void test1(){   int jordan=23;

//method-local inner class

class MLInner{

public void print(){

System.out.println("This is method inner class "+jordan);


}//end of inner class

//Accessing the inner class

MLInner inner=new MLInner();

inner.print();   }

public static void main(String args[]){

Example2 outer =new Example2();



In the above example Example2 is the outer class with MLInner is an inner class defined in its method test1().


When an inner class is declared without a name then is called as anonymous inner class. Anonymous inner class is declared and instantiated at the same time. They are used override the method of a class or interface. We can also pass the anonymous class as an argument to the method.

An anonymous inner class cannot have explicit constructor.

A small example to show how to declare and use anonymous class to override a method and also pass the anonymous inner class as a method argument is given below.

abstract class Example3

{   public abstract void test2();


public class Outer_class {

public static void main(String args[]){

Example3 inner= new Example3(){

public void test2(){

System.out.println("This is an example of anonymous inner class");                    }         };

inner.test2(); }}

Other Example:


interface javatest{

String greet();


public class Example4 {

//method which accepts the object of interface javatest

public void displayMessage(javatest m){

System.out.println(m.greet() +", This is an example of anonymous inner class as an argument");           }

public static void main(String args[]){

//Instantiating the class

Example4 obj=new Example4();

//Passing an anonymous inner class as an argument

obj.displayMessage(new javatest(){

public String greet(){

return "Hello";





One thought on “Inner Classes”

Leave a Reply