S07L06 – Non static inner class in Java continues

Exploring Non-Static Inner Classes in Java – Continued

Table of Contents

  1. Introduction
  2. Recap: What is a Non-Static Inner Class?
  3. Objective of This Chapter
  4. Accessing Inner Class Members
  5. Getter Methods for Inner Class Instances
  6. Restrictions and Visibility of Inner Classes
  7. Step-by-Step Program Walkthrough
  8. Syntax and Output
  9. Key Differences Table
  10. When to Use Non-Static Inner Classes?
  11. Common Interview Questions
  12. 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

Main.java

Syntax Highlight

Output of the Program

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

  1. How is a non-static inner class instantiated in Java?
  2. A non-static inner class is always associated with an instance of its enclosing (outer) class.

    It requires an outer class instance because it can access all non-static members of the outer class.

  3. Can a private inner class be accessed outside?
  4. Yes, but only indirectly using a getter or public methods from the outer class.

  5. 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.
  6. What’s the difference between static and non-static inner classes?
  7. 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
  8. How to access private inner class fields from another class?
  9. Using getters and setters via outer class methods:

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.

Share your love