Exploring Non-Static Inner Classes in Java – Continued
Table of Contents
- Introduction
- Recap: What is a Non-Static Inner Class?
- Objective of This Chapter
- Accessing Inner Class Members
- Getter Methods for Inner Class Instances
- Restrictions and Visibility of Inner Classes
- Step-by-Step Program Walkthrough
- Syntax and Output
- Key Differences Table
- When to Use Non-Static Inner Classes?
- Common Interview Questions
- Conclusion
Introduction
In Java, a Non-Static Inner Class is a class defined within another class (also called the outer class) and is tightly bound to the instance of that outer class. In this continued discussion, we focus on exploring deeper concepts such as access modifiers, instance accessibility, and method invocation from inner class objects.
This section enhances your understanding of encapsulation, object relations, and real-world modeling using Non-Static Inner Classes in Java.
Pros and Cons
Pros | Cons |
---|---|
Inner class can access all members (even private) of the outer class | Can lead to tight coupling |
Helps with encapsulation and modular design | Slightly complex syntax for beginners |
Logically groups classes that belong together | May impact memory usage in large-scale applications |
When to Use
Use non-static inner classes when:
- The inner class needs access to the outer class’s instance variables.
- You want to model logical relationships (e.g., Lock is part of Shop).
- You want to tightly encapsulate functionality.
Recap: What is a Non-Static Inner Class?
A non-static inner class is declared inside another class without the static keyword. It has access to all instance variables and methods of the outer class.
Objective of This Chapter
In this continuation, we focus on:
- Accessing inner class objects from outer class
- Setting and modifying inner class fields
- Using getters and setters properly
- Understanding accessibility issues and how to fix them
Accessing Inner Class Members
From the subtitle, we learned:
- A variable from the inner class Lock named locking is accessed via a public method in the outer class.
- Even though Lock is private, its public methods (isLocking(), setLocking()) can be accessed through getter of Lock object from outer class Shop.
Getter Methods for Inner Class Instances
Java doesn’t allow direct access to private inner class fields from outside. Hence, we use a getter in the outer class to expose the inner class object.
Restrictions and Visibility of Inner Classes
A non-static inner class:
- Can be private: The outer class can still access its members.
- Cannot be accessed outside directly if it’s private, unless exposed via a getter method.
- Is bound to outer class instance, so we can freely access inner members within outer methods.
Step-by-Step Program Walkthrough
Shop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.studeasy; public class Shop { private Lock lock = new Lock(); // Inner class object // Inner non-static class class Lock { private boolean locking = true; public boolean isLocking() { return locking; } public void setLocking(boolean locking) { this.locking = locking; } } public void shopStatus() { if (lock.isLocking()) { System.out.println("Shop is closed."); } else { System.out.println("Shop is open."); } } public Lock getLock() { return lock; } } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 |
import org.studeasy.Shop; public class Main { public static void main(String[] args) { Shop shop = new Shop(); // Outer class object shop.shopStatus(); // First call (should show shop closed) shop.getLock().setLocking(false); // Change inner class field using setter shop.shopStatus(); // Second call (should show shop open) } } |
Syntax Highlight
1 2 3 4 5 6 7 8 |
class Outer { class Inner { // Accesses Outer class members } } Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); |
Output of the Program
1 2 |
Shop is closed. Shop is open. |
Explanation
- Shop is the outer class.
- Lock is a non-static inner class.
- The field locking is set to true by default.
- The shopStatus() method checks if the shop is locked.
- Using getLock().setLocking(false), we change the value to open the shop.
Key Differences Table
Aspect | Static Inner Class | Non-Static Inner Class |
---|---|---|
Requires Outer Object? | No | Yes |
Can Access Outer Members? | Only static members | All members (including private) |
Common Use | Helper or utility classes | Encapsulating tightly coupled classes |
Memory Impact | Light | Heavy |
When to Use Non-Static Inner Classes?
Use Case | Reason |
---|---|
Modeling Object Relationships | e.g., Lock belongs to Shop |
Encapsulation | Keep classes grouped for clarity |
Access to outer fields | No need to pass outer object separately |
Common Interview Questions
- How is a non-static inner class instantiated in Java?
- Can a private inner class be accessed outside?
- Why use getters to access inner class members?
- Encapsulation: Restricts direct access to sensitive data.
- Control and Validation: Allows checks before setting or returning data.
- Access: Exposes private inner class instances safely.
- What’s the difference between static and non-static inner classes?
- How to access private inner class fields from another class?
A non-static inner class is always associated with an instance of its enclosing (outer) class.
1 2 |
Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); |
It requires an outer class instance because it can access all non-static members of the outer class.
Yes, but only indirectly using a getter or public methods from the outer class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Outer { private class Inner { public void display() { System.out.println("Hello from Inner!"); } } public Inner getInner() { return new Inner(); } } Outer outer = new Outer(); Outer.Inner inner = outer.getInner(); inner.display(); |
Feature | Static Inner Class | Non-Static Inner Class |
---|---|---|
Outer class object required? | No | Yes |
Access to outer class members | Only static members | All members |
Usage | Helper / Utility classes | Logically grouped objects |
Memory Impact | Lower | Higher |
Using getters and setters via outer class methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Outer { private class Inner { private String secret = "Hidden Data"; public String getSecret() { return secret; } } public Inner getInner() { return new Inner(); } } Outer outer = new Outer(); Outer.Inner inner = outer.getInner(); System.out.println(inner.getSecret()); |
Conclusion
In this continuation, we explored:
- How to manipulate and access inner class members
- Using getter methods to expose private inner class objects
- Java’s access control and inner class structure
- A working example for better clarity
Understanding how non-static inner classes function provides excellent design capabilities in real-world Java applications.