Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S07L06 - Non static inner class in Java continues

Course Articles
0 views

S07L06 - Non static inner class in Java continues


April 3, 2025

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

ProsCons
Inner class can access all members (even private) of the outer classCan lead to tight coupling
Helps with encapsulation and modular designSlightly complex syntax for beginners
Logically groups classes that belong togetherMay 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

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

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

class Outer {
    class Inner {
        // Accesses Outer class members
    }
}

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();

Output of the Program

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

AspectStatic Inner ClassNon-Static Inner Class
Requires Outer Object?NoYes
Can Access Outer Members?Only static membersAll members (including private)
Common UseHelper or utility classesEncapsulating tightly coupled classes
Memory ImpactLightHeavy

When to Use Non-Static Inner Classes?

Use CaseReason
Modeling Object Relationshipse.g., Lock belongs to Shop
EncapsulationKeep classes grouped for clarity
Access to outer fieldsNo 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.

    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.

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

    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();
    
  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. FeatureStatic Inner ClassNon-Static Inner Class
    Outer class object required?NoYes
    Access to outer class membersOnly static membersAll members
    UsageHelper / Utility classesLogically grouped objects
    Memory ImpactLowerHigher
  8. How to access private inner class fields from another class?
  9. Using getters and setters via outer class methods:

    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.

Tags:
access private inner class
encapsulation Java inner class
inner class getter setter
java inner class example
java nested class access
non-static inner class Java
object relations Java
shop lock example Java
Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.