Advertisement
//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();
}
}
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
Advertisement
//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();
}
}
This is a method of static nested class
//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();
}
}
This is a method of static nested class
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement