Header Ads

How To Use Inner Class or Nested Class in Java?

How To Use Inner Class/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.

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 ClassesInstance Inner ClassesLocal ClassesAnonymous Classes
A static member class can access to all static methods of parent classThis type of class is very much instance specific and it has access to any and all methods and variables.With this reference it can access top level class.This is declared inside a block and accessible within that block only.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 instantiatedThese classes can have some implicit reference to the outer class object once instantiatedThese 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();

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..
new ActionListener()
public void actionPerformed( ActionEvent e )
//do some activities here

Yes!!...you are very correct.This is a very popular example of inner class.
Check out here ..this is a type 3 Anonymous class which has no name. It just tell compiler to crate a class without any name.

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.

That's all from myside...Will update once I will get more such instance.
Powered by Blogger.