< Prev
Next >



Static Nested Classes



Static nested class is a static class that is nested inside another class. Static nested class is not an inner class, it's just a static member of an enclosing outer class, receiving the same priviledges as any other static member of a class does.

How a static nested class is different to an inner class?

Static nested class is not an inner class, as it doesn't get same priviledges as a normal inner classes gets. Static nested class can only access other static members of the class it is enclosed in. While a regular inner class, can access any non-static or static method or member variables of enclosing outer class.

Static Nested class can only access other static members(static methods or static variables) of the enclosing class.

Let's understand this by an example

Here we have created a static nested class, NestedC inside a class, OuterC. Let's see if we can access a non-static member variable(a) of enclosing class, OuterC from within static nested class.
class OuterC
{
int a=10; //outerC member variable


static class NestedC	//Static Nested Class
{
	public void method()
	{
		System.out.println("method of static NestedInner1 class");
		System.out.println("Accessing outer class member variable = "+a);
	}
}
 
 
public static void main(String... ar)
{
NestedC ob= new NestedC();
ob.method();
}

}

Output is :

Static1.java:9: error: non-static variable a cannot be referenced from a static
context
                System.out.println("Accessing outer class member variable = "+a)
;
                                                                              ^
1 error

Program Analysis

While, it is possible for a normal inner class to access a non-static methods or variables of its enclosing class. We have received a compile error when we have tried to access a non-static member(a) of class, OuterC, from within static nested class. Hence it proves that static nested class is not an inner class.



Creating an object of static nested class from within its enclosing class -:

Here we have created a static nested class inside a class OuterC. To access a static member of a class, we don't need its object, the same rule applies when accessing a static nested class. Let's see :
class OuterC
{
static class NestedC
{
	public void nestedMethod()
	{
		System.out.println("method of static nested class class");
	}
}
 
 
public static void main(String... ar)
{
NestedC ob= new NestedC();
ob.method();
}

}

Output is - :

method of static nested class

Program Analysis




Creating an object of static nested class from outside of its enclosing class -:

Here we have created a static nested class inside a class OuterC. To access a static member of a class from outside of its enlosing class, we need to access it with its class name, the same rule applies when accessing a static nested class from outside its enclosing class. Let's see :
class B
{
static class NestedC
{
	public void nestedMethod()
	{
		System.out.println("method of static nested class");
	}

}
}



class A
{
public static void main(String... ar)
{
	B.NestedC ob2 = new B.NestedC();
	ob2.method();
}
}

Output is - :

method of static NestedInner2 class

Program Analysis




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Method Local Inner Class
Anonymous Inner Class >
Please subscribe for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
JSP & Servlets

Ad2