About Lesson
Java Inner class
In Java, an inner class is also known as nested class. Inner classes are part of nested classes. When a non-static class is defined in nested class then it is known as an inner class. It is defined inside the class or an interface. Inner classes are mostly used to logically group all the classes and the interface in one place, which makes the code more readable and manageable. Inner classes can access members of the outer class including all the private data members and methods.
Syntax:
Types of Nested classes
- Non-static nested class
- Member inner class
- Anonymous inner class
- Local inner class
- Local inner class
Following are the examples in which inner classes can be defined
1. static inner class with a static method
Example:
Â
Â
2. static inner class with non-static method
Example:
Â
3. non-static inner class with a non-static method
Example:
Â
4. non-static inner class with a static method
Note: it is an illegal combination. Only static variables are allowed and should be final.
Example:
Â
5. Nested inner class in local method
Example:
Â