Inner Nested Classes

Non-static nested classes are slightly different from static nested classes,
a non-static
nested class is actually associated to an object rather than to the class in
which it is nested.
Any member of the inner nested class
is not a part of its outer class while its source code is in the class definition.
Non-static members of a class specify the requirements of objects created from that class. Every object has a copy of the nested class that belongs to the
outer class. The same copy should access all the methods and instance variables of
that object.
Methods and variables of an inner
class can be access directly by the object of outer class. The figure given below illustrates it logically.

Lets try to explain the concept of
inner nested class with the help of an example. There is an inner class
company exists within an outer class Info. The class Info is to
be referred as non-static nested class. The class Info invokes the method of
the class Company with the help of the object of the class Company.
Here is the code of the Example :
Info.java
import java.lang.*;
public class Info{
static String compName="Rose India";
public static class Company{
int time=10;
void showinfo(){
System.out.println("Our Company Name : "+compName);
System.out.println("The time of the company : "+time);
}
}
public static void main(String[] args){
Info.Company object = new Info.Company();
object.showinfo();
}
}
|
Here is the output of the Example :
C:\roseindia>javac Info.java
C:\roseindia>java Info
Our Company Name : Rose India
The time of the company : 10
|
Download this Example:

|