Static and Non Static Inner Class in Java

0
160
Spread the love
  • 24
  • 34
  • 14
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    107
    Shares

Static and Non Static Inner Class in Java
Static and Non Static Inner Class in Java

Static and Non Static Inner Class in Java:

It is possible in java to put a class definition within another class definition. The enclosing class is called outer class and the class inside is called inner class.This technique allows us to group classes that logically belong together and to control the visibility of the one within other.The inner classes are different from Composition.

Inner classes can not be accessed directly until we have create an object for the outer class.
Inner classes can be classified as:

  1. Non Static Inner Class
  2. Static Inner Class
There are other inner classes as well. read here for more.

Non Static Inner Class:

The generic syntax for Non Static Inner class:


OuterClassName.InnerClassName <<Object/handle>>=OuterClassHandle.new innerClassName();
 

An example of Inner class:


public class Parcel{
       class Content{
       private int i=11;
       public int value(){return i;}
       }
       class Destination{
       private String address;
       Destination(String add){address=add;}
       String getAddress(return address;}
       }
public static void main(String args[])
{
Parcel p=new Parcel();
Parcel.Content c=p.new Content();
Parcel.Destination d=p.new Destination("India");
}
}
 

Here the class Destination and Content are hidden inside the Parcel Class. Parcel is the outer class and these two are hidden class.


Parcel p=new Parcel();
 

This code creates a handle for outer class and to create handle for inner class and to get the inner class we need to use:


Parcel.Content c=p.new Content();
 

 

We can not access the inner class by simply creating the outer class handle,we must use the instance of the outer class to create instances of inner class and then to access it.

Outer class may have method that returns a handle to an inner class:


public Content cont(){return new Content();}
//in that case we can define-
Parcel p=new Parcel();
Parcel.Content c=p.cont();
//for destination
public Destination dest(String s){
return new Destination(s);
}
Parcel.Destination d=p.dest("India");
 

Static Inner Class:

By static inner class, we do not need to create object of the outer class for creating object of the inner class.Instead we can call the methods or create object using the generic syntax given below:

Generic Syntax for static Inner Class:


OuterClassName.methodName();
 

An example of static Inner Class:


abstract class Content{
abstruct public int value();
}
interface Destination{
String getAddress();
}
public class Parcel{
private static class PContenet extends Content{
private ingt i=11;
public int value(){return i;}
}
protected static class PDestination implements Destination{
private String address;
private PDestination(String add)
{
address=add;
}
public String getAddress(){return address;}
}
public static Destination dest(String str)
{
return new PDestination(str);
}
public static Content con()
{
return new PContent();
}
public static void main(String args){
Content c=Parcel.con();
Destination d=Parcel.dest("India");
}
}
 

 

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
  • 24
  • 34
  • 14
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    107
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here