![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Nesting of class is process of writting one class into another class. sometimes it is necessary to group the classes logically.
Nesting of class increases the use of encapsulation in java to create more effective program.
Syntax
class FirstClass { ... ... class SubFirstClass { ... ... } }
The class which is created inside the class is static Nested class. It cannot access non-static members but outer class can access it.
Example
Printing the values of class using static Nested class
class StaticClassDemo { static int value = 50; static class InnerClass { void printValue() { System.out.println("Value is : "+value); } } public static void main(String args[]) { StaticClassDemo.InnerClass inner = new StaticClassDemo.InnerClass(); inner.printValue(); } }
Output