Advertisement



< Prev
Next >



Java - Static Nested Classes





Static nested class is a static class that is nested inside another class. Static nested class is not an inner class, but 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 the same privileges as a normal inner class gets. Static nested class can only access the other static members of a 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.




Example of how a static nested class is different from an inner class.


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, like we could do from an inner class.

//Java - How a static nested class is different from inner class.


class OuterC
{
int a=10; 		//OuterC member variable


static class NestedC	//Static Nested Class
{
	public void method()
	{
		System.out.println("This is a method of static nested class");
		System.out.println("Accessing an 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 an 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.

//Java - Example of static nested class
 
 
class OuterC
{
static class NestedC	//Static Nested Class
{
	public void nestedMethod()
	{
		System.out.println("This is a method of static nested class");
	}
}
 
 
public static void main(String... ar)
{
	NestedC ob= new NestedC();
	ob.method();
}

}


Output is - :


This is a 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.

//Java - Example of static nested class
 

class B
{
static class NestedC	//Static Nested Class
{
	public void nestedMethod()
	{
		System.out.println("A method of static nested class");
	}

}
}



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


Output is - :


This is a method of static nested 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