Results for Innerclass

Static and Non Static Inner Class in Java

January 10, 2019
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");
}
}
 

Static and Non Static Inner Class in Java Static and Non Static Inner Class in Java Reviewed by Animesh Chatterjee on January 10, 2019 Rating: 5

How To Use Inner Class or Nested Class in Java?

January 02, 2013
How To Use 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 ClassesInstance Inner ClassesLocal classesAnonymous Classes
A static member class can access to all static methods of parent classThese 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 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.
How To Use Inner Class or Nested Class in Java? How To Use Inner Class or Nested Class in Java? Reviewed by Animesh Chatterjee on January 02, 2013 Rating: 5
Powered by Blogger.