member inner classes in java
Member inner classes in Java are non-static inner classes that are defined within another class, known as the outer class. Unlike static nested classes, member inner classes have access to instance variables and methods of the outer class, as well as other members of the outer class, without needing an instance of the outer class. Here’s a detailed explanation:
Table of Contents
1. Definition:
- Member inner classes, also known as non-static inner classes, are defined within the body of another class, known as the outer class.
- They are declared at the member level of the outer class and are not marked as `static`.
- Member inner classes have access to the instance variables and methods of the outer class.
2. Access to Outer Class Members:
- Member inner classes have implicit access to all members (fields, methods, and other inner classes) of the outer class, including private members.
- They can access instance variables and methods of the outer class directly, without needing an instance of the outer class.
3. Instance Relationship:
- Instances of member inner classes are implicitly associated with instances of the outer class.
- They cannot exist without an instance of the outer class and have access to the instance variables and methods of the outer class through an implicit reference.
4. Visibility and Scope:
- Member inner classes are scoped to the outer class and cannot be accessed from outside the outer class.
- They are typically used to encapsulate functionality closely related to the outer class, promoting encapsulation and information hiding.
5. Declaration Syntax:
- Member inner classes are declared within the body of the outer class, similar to other members such as fields and methods.
- They are instantiated using an instance of the outer class, and their constructors may have an implicit reference to the outer class instance.
6. Usage:
- Member inner classes are commonly used for grouping related functionality within the scope of the outer class.
- They are useful for implementing complex data structures, providing specialized behavior, or implementing interfaces within the context of the outer class.
```java
public class OuterClass {
private int outerField;
// Member inner class
public class InnerClass {
private int innerField;
public void innerMethod() {
outerField = 10; // Accessing outer class instance variable
System.out.println("Inner method of InnerClass");
}
}
public static void main(String[] args) {
// Instantiate the outer class
OuterClass outerObj = new OuterClass();
// Instantiate the inner class using the outer class instance
OuterClass.InnerClass innerObj = outerObj.new InnerClass();
// Accessing the inner class method
innerObj.innerMethod();
}
}
```
In this example, `InnerClass` is a member inner class of `OuterClass`. We create an instance of `InnerClass` using an instance of `OuterClass`, and then we can access the methods and fields of `InnerClass` as well as the instance variables and methods of `OuterClass` through an implicit reference. Member inner classes are scoped to the outer class and cannot be accessed directly from outside the outer class.