In large systems, some components are logically related to a parent entity but do not require its state to function. This project demonstrates Static Nested Classes in Java. By declaring BasicCharm as static within Spellbook, we decouple the two. This allows the instantiation of BasicCharm without creating a Spellbook object, saving memory and simplifying the architecture when instance-specific data is not required.
- Static Nesting: Defined
BasicCharmas a static member ofSpellbook. - Independent Instantiation: Demonstrated the
new Outer.StaticNested()syntax which does not require an outer instance. - Method Execution: Implemented
castSpell()to verify the independent lifecycle of the nested class. - Namespace Organization: Used the outer class primarily as a logical container (namespace).
- Java 8+ (Static Nested Classes, Decoupled Design)
- Spellbook: The outer namespace representing the collection.
- BasicCharm: The independent static nested class.
- SpellbookApp: The entry point demonstrating creation of the charm without a book.
Hello from static basic charm!
Project Structure:
JavaBasics_Task_268/
├── src/
│ └── com/yurii/pavlenko/
│ ├── Spellbook.java
│ └── SpellbookApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class SpellbookApp {
public static void main(String[] args) {
Spellbook.BasicCharm charm = new Spellbook.BasicCharm();
charm.castSpell();
}
}package com.yurii.pavlenko;
public class Spellbook {
public static class BasicCharm {
public void castSpell() {
System.out.println("Hello from static basic charm!");
}
}
}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