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:


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 :

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>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.

Share on Google+Share on Google+

Static Nested Classes

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



Discuss: Static Nested Classes   View All Comments

Post your Comment

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