Share on Google+Share on Google+

Inner Nested Classes

Advertisement
Non-static nested classes are slightly different from static nested classes, a non-static nested class is actually

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.

An InnerClass Exists Within an Instance of OuterClass.

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:

Advertisements

Posted on: May 27, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Inner Nested Classes   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0