Last Updated: 04 February, 2023
A class that is defined inside another class without a static keyword and outside a method is known as a "member inner class" in Java.
Syntax of Member Inner Class:
To access inner class properties first, we need to create an instance of an outer class because the inner class is defined as a member of the outer class. Using the outer class instance, we create the instance of the inner class.
Let's understand the implementation of member inner class with the help of the given example.
Example: Member Inner Class in Java
Output
Outer Class - display() method
Name : John
City : Bengaluru
Outer Class - display() method
What compiler does at compile time:
When we compile the above Java program, after successful compilation, the compiler generates three .class files for the each classes. These files are named by the compiler as:
👉 Before Compilation
👉 After Compilation
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Can inner classes access private members of outer class?
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.
How many inner classes we can define inside the outer class?
We can define any number of inner classes in the outer class; there is no programmatic limit, but inner classes should be defined based on the project's business requirements.