Static and Non Static Inner Class in Java

digitization, transformation, binary
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 the 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.

When we define a class within another class, it is defined as nested class. There are two types of the nested class.

Ads code goes here

Inner classes can not be accessed directly until we have created 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:

A non-static nested class can access all of the variables and methods of its outer class and can also refer to them directly.

The generic syntax for Non-Static Inner class:


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

An example of Non-Static 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. The 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 a 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 access it.

Outer class may have a 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");
 

One more example of non-static inner class:

Class MyOuterclass {
 Class MyInnerclass {
  int inner_member = 9;
  MyInnerClass() {
   Outer_member = 10;
   System.out.printh(outer_member);
  }
 }
 Private int outer_member:
  MyOuterClass() {
   inner_member = 12: // error
    MyInnerclass mic = new MyInnerclass():
    System.out.printh(mic.inner_member):
  }
}
Class TestMyInnerClass {
 Public static void main(string args[]) {
  MyOuterClass moc = new MyOuterClass():
 }
}

Static Inner Class:

If a nested class has a static modifier applied and the member of the enclosing class can be accessed through an object, then the class is called Static Nested Class. It cannot refer to members of its enclosing class directly. Due to this restriction, they are hardly used.

By static inner class, we do not need to create an object of the outer class for creating an object of the inner class. Instead, we can call the methods or create an 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");
}
}
 

One more example of static Inner class

Class MyOuterClass {
 static class MyInnerclass {
  int inner_member = 12
  MyInnerClass() {
   Outer_member = 10: // error
    System.out.printh(outer member); //error
   MyOuterClass moc = new MyOuterClass();
   Moc.outer_member = 15;
   System.out.printh(moc.outer_member);
  }
 }
 private int outer_member;
 Void innerPrinter() {
  MyInnerClass mic = new MyInnerClass();
  System.out.printh(mic.inner_member);
 }
}
Class TestMyInner class {
 public static void main(String[] args)
  MyOuterClass moc = new MyOuterClass();
 moc.innerPrinter();
 }
}

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *