Advertisement



< 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 privileges as any other static member of a class does.




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


Static nested class is not an inner class, as it doesn't get same privileges 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 its enclosing outer class.




Note:


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




Static nested class example


In the upcoming example, we have created a static nested class, NestedC, inside a normal class, OuterC. Let's see if we can access a non-static member variable of the enclosing class, OuterC, from within its static nested class, NestedC.

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.


Advertisement




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


In the next example, we have created a static nested class, NestedC, inside a normal class, OuterC. As you must remember that we don't need an object of class to access its static member, this same rule also applies when accessing a static nested class. Let's see the example.

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


As you must remember that we can access a static member of a class from the outside of its enclosing class, by using its class name with the dot operator.

The same rule applies when accessing a static nested class from the outside of its enclosing class. Let's see how it is done.

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 -




< Prev
Next >
< Method Local Inner Class
Anonymous Inner Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement