In object-oriented design, some entities are so closely related that one cannot exist without the other. This project demonstrates the use of Non-static Inner Classes in Java. By nesting the Label class inside the Box class, we create a strong logical bond. An instance of Label is always tied to a specific instance of Box, reflecting real-world scenarios where a label is a physical part of a specific package.
- Inner Class Implementation: Defined the
Labelclass within the scope of theBoxclass. - Instance Dependency: Demonstrated that a
Boxobject is required to instantiate aLabel. - Method Access: Implemented the
printLabel()method within the nested context. - Encapsulation: Showed how to reference inner types using the
Outer.Innersyntax.
- Java 8+ (Inner Classes, Object Coupling)
- Box: The outer class representing the physical container.
- Box.Label: The inner class representing the identification tag.
- SolutionApp: The entry point managing the creation and linkage of these components.
Box label
Project Structure:
JavaBasics_Task_264/
├── src/
│ └── com/yurii/pavlenko/
│ ├── Box.java
│ └── SolutionApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class SolutionApp {
public static void main(String[] args) {
Box box = new Box();
Box.Label label = box.new Label();
label.printLabel();
}
}package com.yurii.pavlenko;
public class Box {
public class Label {
public void printLabel() {
System.out.println("Box label");
}
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT