Inner class in java

Java Inner Class

 
In this article, we will discuss about :
 

  • Nested classes
  • Types of Nested classes
  • Difference between nested class and inner class
  • Java Inner classes
  • Inner class example
  • Creating inner class instance from outer class instance method
  • Creating inner class instance from outside outer class
  • Creating inner class instance from outer class static method
  • Advantage of Inner class
  • Disadvantage of Inner class

 
 

Nested Class in java

 
When we define one class inside another class, its called a Nested class.
 
The nested class can not exist independently and is always dependent on the Outer class.
 
Java Inner class
 

Types of Nested classes

 
There are two types of nested class :
 

1) Static Nested Class

 
In this case, the nested class is marked as static. This class can’t access non-static members of outer class.
 

2) Non-static Nested Class

 
Non-static nested class is also called Inner Class.
 
Java inner class definitions
 

Difference between Nested class and Inner class in Java

 
Inner class is a type of nested class. Non-static nested classes are known as inner classes.
 
Nested class can also be a Static Nested class.
 
 

Inner Class

 
Java inner class is a class defined inside another class.
 
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
 
It can access all the members of outer class including private data members and methods.
 
 

Syntax of Inner class

 


class OuterClass{  
 // Outer class code
    class InnerClass{   
       // Inner class code
    }  
}  

 
 

Creating Inner class instance from Outer class instance method(not Static)

 
Outer class members see the Inner class as a normal class.
 
So, you can just call new MyInner().innerMethod().
 

public class MyOuter {
  
  private int x =5;
  
  
  public static void main(String[] args){
    MyOuter outer = new MyOuter();
    outer.outerMethod();
  }
  
  //Aceesing inner class from outer class instance method
  private void outerMethod(){
    MyInner inner = new MyInner();
    inner.innerMethod();
  }
  
  class MyInner{
    public void innerMethod(){
      System.out.println("From Inner method, x = " + x);
    }
  }

}

 

Output:

 
From Inner method, x = 5
 
 

Creating Inner class instance from outside the Outer class or from an Outer class’s static method

 
From outside the Outer class code & from Outer class’s static methods, inner class can be accessed using Outer class name.
 

package com.topjavatutorial;

public class Outer {
  
  private int x =5;
  
  //Aceesing inner class from static method
  public static void main(String[] args){
    Outer.Inner inner = new Outer().new Inner();
    inner.innerMethod();
  }
  
  class Inner{
    public void innerMethod(){
      System.out.println("From Inner method, x = " + x);
    }
  }

}


 

Output:

 
From Inner method, x = 5
 
 

Advantages of java inner classes

 
Here are some advantages of inner classes in java :
 
1) Inner classes represent a special type of relationship with outer class; i,e. it can access all the members (data members and methods) of outer class including private.
 
2) The inner class is supposed to be used where it is useful in context of outer class object only. For example, Node class can be relevant to LinkedList only. So, it makes sense to put it as an Inner class.
 
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
 

 

Disadvantages of Inner class in java

 
Inner class can be difficult to understand by relatively new programmers.
 
 

Next

 
Read about the different inner class types in following posts :

Method Local Inner class in java
 
Anonymous Inner class in java
 
Static Nested class in java
 
 

Puzzles on Inner classes

 
Java Quiz 26
 
Java Quiz 27
 
 

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags