How To Use Inner Class or Nested Class in Java?

0
88
Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Inner Class or Nested Class in Java

How To Use Inner Class or Nested Class in Java?

Inner Class is popularly known as nested class. As the name suggested, inner class means a class or a group of classes within a class.Basically these are helper class.

Example could be–


 class mainClass{ 
......
class subClass1{
......
}
class subclass2{
......
}
} 

 

Nested classes are mainly two types–

1. Static–those inner classes declared as static called static nested classes
2. Non Static–those inner classes are declared as other than static called inner class.
Read in details of Static and Non static Inner classes here

Non Static class can be further divided into 3 types

1. Instance Inner class:
2. Local Inner class
3. Anonymous inner class

Above given code is an example of non static inner class.

Static inner class example could be–


class mainClass{
......
static class subClass1{
......
}
class subclass2{
......
}
}  
 

What is the difference between two classes?

Well, non static nested classes popularly called inner classes can access to other members of the enclosing classes but a static class can’t.You can declare an inner class within the body of a method. Such a class is known as a local inner class. You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes.
Nesting classes are nothing but to create relationship between classes.

Static Member Classes Instance Inner Classes Local classes Anonymous Classes
A static member class can access to all static methods of parent class These are local classes that has no name
Static Member class does not have implicit reference to outer class as it is static. These classes can have some implicit reference to the outer class object once instantiated These classes can have some implicit reference to the outer class object once instantiated

 

Then why we will use Nested Classes?

if you are doing complex UI design and implementing complex algorithms nested class allows you to do —
1. Logical grouping–all classes in one place so it increases the maintainability.
2. More encapsulation–more hiding but create a great package organization
3. White box testing becomes easy and code becomes truly object oriented.
4. Provides a classic callback to the methods defined inside these classes.

To instantiate an inner class, we must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:


OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
 

For static class the mechanism will be


OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 
 

Most popular Inner class example..


 public class OuterClass {
private String outerString="Hello from outer";
class inner {
String innerString="Hi from inner" ;
void callBack() {
System.out.print("Calling from Inner"+innerString);

}
}
}

Now let me create the main class from where I will access the inner class..


public class TestClass {
public static void main(String[] args) {
OuterClass oC = new OuterClass();
OuterClass.inner ii=oC.new inner();
ii.callBack();
}
}

 

This will call the callBack method to perform activity.

Now let us come to the second most used inner class.
Do you remember the code, we used to memorize during our fist assignment…to add an action listener to a button..


Button1.addActionListener
(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
//do some activities here
}
}
)  

 

Disadvantages:

1. From the novice programmer’s point of view these type of classes and callback method may be tough to understand
2. Maintainability becomes tough if the code is created in one java editor and opened in other editor. This is due to editor’s incompatibilities.

Class Identifiers:

All classes produces a .class file after compilation. So as the Inner classes. It follows a strict formula to identify the inner class. The formula is as given below:

outerClass$innerclass.class
// so in the same location where the outer class exists there will be two classes:
outerClass.class
outerClass$innerclass.class
 

These classes are also platform independent in nature.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here