Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Static Nested Classes 
 

A nested class that is declared static is called a static nested class.

 

Static Nested Classes

                         

A nested class that is declared static is called a static nested class. Memory to the objects of  any static nested classes are allocated independently of any particular outer class object. A static nested class use the instance variables or methods defined in its enclosing class only through an object reference. A static nested class interacts with the instance members of its outer class or any other class just like a top-level class.

Given below is the syntax of the Static nested class that defines static nested class having keyword static in the outer class.

class OuterClass {
  ....
static class StaticNestedClass {
  ....
}
  class InnerClass {
  .... }
  }

Static nested classes can be accessed by using the enclosing class name:

OuterClass.StaticNestedClass

If we want to make an object of the static nested class then we have to write down the following code:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Here is an example of the Static nested class that processes of accessing the instance of the outer class inside the inner class. The example creates an "Outer" class with an instance x of value 100 and after that we call this value in inner class method check. Apart from that the example also creates another function check and call the inner class check() method inside it. When the example calls the check() with Outer class, it shows the value of Outer class instance x.

Here is the code of the Example :

Outer.java

import java.lang.*;

public class Outer{

  int x = 100;

  class Inner{

  int x = 200;

    public void check(){

     System.out.println("Value of x is: "+ Outer.this.x );

    }

  }

  public void check(){

    new Inner().check();

    }

    public static void main(String args[]){

      new Outer().check();

    }

}

Here is the output of the Example :

C:\roseindia>javac Outer.java

C:\roseindia>java Outer
Value of x is: 100

Download this Example:

The advantage of a static nested class is that it doesn't need an object of the containing class to work. This can help you to reduce the number of objects your application creates at runtime.

                         

» View all related tutorials
Related Tags: c class io nested definition source while ini member e il it not init art ce in no part as

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.