S07L05 - Non static inner class in Java
Mastering Non-Static Inner Classes in Java
Table of Contents
- Introduction
- What is an Inner Class in Java?
- Types of Inner Classes
- Non-Static Inner Class Explained
- Real-World Analogy
- Syntax of Non-Static Inner Class
- Program Code Explanation (from project)
- Step-by-Step Code Breakdown
- Output Explanation
- Comparison Table: Static vs Non-Static Inner Class
- When to use Non-Static Inner Class?
- Common Interview Questions on Non-Static Inner Classes
- Conclusion
Introduction
Inner classes are a powerful concept in Java that allow you to define a class inside another class. This feature is frequently used to logically group classes, enhance encapsulation, and establish tight coupling between related classes.
In this tutorial, we will focus on Non-Static Inner Classes — the most widely used type of inner class. We will walk through:
- What inner classes are
- How non-static inner classes work
- Hands-on example using a real-world scenario
- Complete code walkthrough
- Interview Q&A
What is an Inner Class in Java?
An inner class is a class defined within another class. It is also known as a nested class. This technique helps organize code logically and access the outer class's members directly.
Types of Inner Classes
| Type | Description |
|---|---|
| Non-Static Inner Class | A class defined inside another class without the static keyword |
| Static Inner Class | A nested class marked with the static keyword |
| Local Inner Class | Defined inside a method or block |
| Anonymous Inner Class | Class without a name, usually for immediate object creation |
Non-Static Inner Class Explained
A Non-Static Inner Class is closely associated with its enclosing (outer) class object. It can directly access all members (including private) of the outer class.
Key Characteristic: You need an instance of the outer class to instantiate the non-static inner class.
Real-World Analogy
Imagine a Shop that has a Lock. A lock cannot exist without the shop. The lock is dependent on the shop's existence. Similarly, a non-static inner class cannot exist without the outer class.
Syntax of Non-Static Inner Class
class Outer {
class Inner {
// Inner class members
}
}
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Program Code Explanation (from project)
Shop.java
package org.studeasy;
public class Shop {
private Lock lock = new Lock(); // creating instance of inner class
// Inner non-static class
class Lock {
private boolean locking = true; // private variable
// Getter
public boolean isLocking() {
return locking;
}
// Setter
public void setLocking(boolean locking) {
this.locking = locking;
}
}
// Method to check shop status
public void shopStatus() {
if (lock.isLocking()) {
System.out.println("Shop is closed.");
} else {
System.out.println("Shop is open.");
}
}
// Getter for Lock object
public Lock getLock() {
return lock;
}
}
Main.java
import org.studeasy.Shop;
public class Main {
public static void main(String[] args) {
Shop shop = new Shop(); // create Shop object
shop.shopStatus(); // check shop status
// Unlocking the shop
shop.getLock().setLocking(false);
// Check status again
shop.shopStatus();
}
}
Step-by-Step Code Breakdown
- Shop Class has a non-static inner class Lock.
- Lock holds a private variable locking initialized to true.
- shopStatus() method checks if the shop is locked or not.
- Main class creates an instance of Shop.
- Initially, shopStatus() prints "Shop is closed."
- We then unlock the shop using the setter setLocking(false).
- The second call to shopStatus() prints "Shop is open."
Output Explanation
Shop is closed. Shop is open.
Comparison Table: Static vs Non-Static Inner Class
| Feature | Static Inner Class | Non-Static Inner Class |
|---|---|---|
| Requires outer class object? | No | Yes |
| Can access outer class members | Only static members | Both static and non-static members |
| Memory Consumption | Lower | Higher |
| Common Use Case | Utility/helper classes | Tight coupling with outer class instance |
When to use Non-Static Inner Class?
| Situation | Reason to use |
|---|---|
| When inner object is logically part of the outer object | E.g., Lock is part of Shop |
| When inner object needs direct access to outer class fields | Can access even private members |
| To encapsulate helper classes | Keeps classes tightly grouped |
Common Interview Questions on Non-Static Inner Classes
- What is a non-static inner class in Java?
- How does a non-static inner class access outer class members?
- Can you instantiate a non-static inner class without an outer class object?
- What's the difference between static and non-static inner classes?
- How does Java handle access modifiers inside inner classes?
- Why would you use an inner class instead of a separate class?
Conclusion
In this article, you have learned:
- The fundamentals of Inner Classes
- How to create and use Non-Static Inner Classes
- Real-world usage through the Shop-Lock example
- Step-by-step code walkthrough
- Differences between static and non-static inner classes
- Interview questions to help you prepare
Non-static inner classes provide powerful design flexibility when used properly. Always prefer them when the inner class logically belongs to the outer class and needs full access to it.