S07L05 – Non static inner class in Java

Understanding Non-Static Inner Classes in Java

Table of Contents

1. Introduction

In Java, classes can be declared within other classes, referred to as inner classes. One important type of inner class is the non-static inner class, which has a direct association with its enclosing class.
In this chapter, we will explore the non-static inner class, its purpose, and how to implement it with practical examples.

Understanding non-static inner classes is crucial for developers who wish to create more complex and logically encapsulated code.
By the end of this guide, you’ll know how to declare, instantiate, and work with non-static inner classes, as well as understand the advantages and limitations of using them in Java applications.

2. What Are Non-Static Inner Classes?

A non-static inner class is a class that is defined within another class but without the static keyword.
This means that an instance of the inner class can only exist in association with an instance of the outer class.

Aspect Non-Static Inner Class Static Inner Class
Association with Outer Class Requires outer class instance No association required
Access to Outer Class Members Can access both static and non-static members Can only access static members
Memory Management Memory consumed for each instance of the outer class Memory is not dependent on outer class instances

When to Use Non-Static Inner Classes:

  • Use when the inner class logically belongs to the instance of the outer class.
  • When the inner class needs access to the instance-specific data or methods of the outer class.

3. Creating and Using Non-Static Inner Classes

The syntax for creating a non-static inner class in Java is straightforward. A non-static inner class is declared inside another class, and its objects are created using the instance of the outer class.

Syntax of Non-Static Inner Class

Accessing Non-Static Inner Class

To instantiate the inner class, you need to first instantiate the outer class. Here’s how you do it:

4. Example: Shop and Lock Implementation

Let’s take a practical example to illustrate how non-static inner classes work. The following Java class, Shop, uses a non-static inner class Lock to represent whether the shop is open or closed based on the lock status.

Code Explanation:

  • Outer Class (Shop): This class represents a shop, which contains a lock object.
  • Inner Class (Lock): This non-static inner class manages the state of the shop’s lock, determining whether the shop is open or closed.
  • Methods:
    • shopStatus(): Checks the status of the lock to print whether the shop is open or closed.
    • isLocking() and setLocking(): Getter and setter for the lock state.

Output of the Code:

5. Key Concepts and Terminology

Non-Static Inner Class: A class defined inside another class without using the static keyword.

Outer Class: The class that contains the inner class.

Inner Class Instantiation: A non-static inner class must be instantiated via an instance of the outer class.

Advantages:

  • Encapsulation: Grouping the inner class logically with its outer class.
  • Access: Inner classes have full access to the outer class’s members, including private ones.

Disadvantages:

  • Performance: Requires additional memory and resources since it is tightly bound to the outer class instance.
  • Complexity: Can add unnecessary complexity if overused.

6. Conclusion

Non-static inner classes in Java allow developers to logically group classes that are closely associated with each other. They provide a clean mechanism to encapsulate functionality that is only relevant in the context of the outer class. However, developers must be cautious of the memory and performance implications.

By understanding the practical example of the Shop and Lock classes, you can now create more modular and structured Java applications, utilizing the power of non-static inner classes.