Java Inner Classes
Java inner class or nested class is a class which is declared inside the class or interface
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
Type |
Description |
Member Inner Class |
A class created within class and outside method.
|
Anonymous Inner Class |
A class created for implementing interface or extending class. Its name is decided by the java compiler. abstract class Person{ |
Local Inner Class |
A class created within method. public class localInner1{ private int data=30;//instance variable class Local{ Local l=new Local(); public static void main(String args[]){ |
Static Nested Class |
A static class created within class. class TestOuter1{ |
Nested Interface |
An interface created within class or interface.
|