Java Static Nested Class
Chapter:
Miscellaneous
Last Updated:
25-03-2017 06:06:57 UTC
Program:
/* ............... START ............... */
public class NestedStaticDemo
{
private static int age = 27;
private int salary = 45000;
static class NestedStaticClass
{
void showAge()
{
System.out.println("Age is : " + age);
}
void showSalary()
{
//Below statement will give compile-time error as salary is not static variable.
//System.out.println("Salary is : " + salary);
}
}
public static void main(String[] args)
{
NestedStaticDemo.NestedStaticClass obj = new NestedStaticDemo.NestedStaticClass();
obj.showAge();
}
}
/* ............... END ............... */
Notes:
-
In java, a static class thats is created inside another class is called static nested class. Static nested class cannot access non-static members of it’s outer class.
Tags
Static Nested Class, Java, Miscellaneous