Skip to content

Commit e3a3fcd

Browse files
authored
Update oop.md
1 parent 2eadb2d commit e3a3fcd

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

oop.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class Implementation implements A, B {
501501
**Q:** What happens if a class implements two interfaces with conflicting default methods?
502502
**A:** Compile error — the class must override the conflicting method to explicitly resolve which implementation to use or provide its own.
503503

504-
## 🃏Nested Classes vs Inner Classes - Key Distinctions
504+
## 🃏 Nested Classes vs Inner Classes - Key Distinctions
505505

506506
**Rule:** Inner class = NON-STATIC nested class. All inner classes are nested, but not all nested classes are inner.
507507

@@ -514,6 +514,8 @@ class Implementation implements A, B {
514514
* Local inner class
515515
* Anonymous inner class
516516

517+
**Important:** Classes inside interfaces, nested enums, and nested records are **implicitly static** (not inner classes). The "static" keyword only applies to nested types, never top-level types.
518+
517519
**Code Examples:**
518520

519521
```java
@@ -570,6 +572,29 @@ interface MyInterface {
570572
System.out.println("Inside interface nested class");
571573
}
572574
}
575+
576+
// Nested enum - implicitly static
577+
enum Status { ACTIVE, INACTIVE } // Cannot be inner class
578+
579+
// Nested record - implicitly static
580+
record Data(String name, int value) { } // Cannot be inner class
581+
}
582+
583+
// Class with nested enum and record
584+
public class OuterWithSpecialTypes {
585+
private String field = "outer";
586+
587+
// Nested enum - always implicitly static
588+
enum Priority { HIGH, MEDIUM, LOW }
589+
590+
// Nested record - always implicitly static
591+
record Person(String name, int age) { }
592+
593+
void testAccess() {
594+
// These cannot access instance fields because they're implicitly static
595+
// Priority.HIGH cannot access 'field'
596+
// Person cannot access 'field' in its methods
597+
}
573598
}
574599
```
575600

@@ -588,6 +613,12 @@ OuterClass.MemberInnerClass inner2 = new OuterClass().new MemberInnerClass();
588613

589614
// Interface nested class - implicitly static
590615
MyInterface.NestedInInterface interfaceNested = new MyInterface.NestedInInterface();
616+
617+
// Nested enum - implicitly static (no outer instance needed)
618+
OuterWithSpecialTypes.Priority priority = OuterWithSpecialTypes.Priority.HIGH;
619+
620+
// Nested record - implicitly static (no outer instance needed)
621+
OuterWithSpecialTypes.Person person = new OuterWithSpecialTypes.Person("John", 30);
591622
```
592623

593624
**Access Rules Summary:**
@@ -628,10 +659,18 @@ Outer.Inner correct = new Outer().new Inner(); // ✅ Correct
628659

629660
// Pitfall 3: Interface nested class confusion
630661
interface Test {
631-
class Helper { } // Implicitly static, not inner!
662+
class Helper { } // Implicitly static, not inner!
663+
enum Status { ON } // Implicitly static, not inner!
664+
record Info(String data) { } // Implicitly static, not inner!
665+
}
666+
667+
// Pitfall 4: Thinking you can make enum/record inner classes
668+
class BadExample {
669+
// enum MyEnum { } // Always implicitly static - cannot be inner!
670+
// record MyRecord() { } // Always implicitly static - cannot be inner!
632671
}
633672

634-
// Pitfall 4: Static nested accessing instance members
673+
// Pitfall 5: Static nested accessing instance members
635674
class Example {
636675
String field = "test";
637676
static class BadNested {
@@ -642,7 +681,7 @@ class Example {
642681
}
643682
```
644683

645-
**💡 Learning Tip:** "Static nested = NO instance access, Inner = FULL access" - Static nested classes can't access instance members, while inner classes can access everything in the outer class.
684+
**💡 Learning Tip:** "Static nested = NO instance access, Inner = FULL access" - Static nested classes (including implicitly static enums/records/interface classes) can't access instance members, while inner classes can access everything in the outer class. Remember: "static" keyword only applies to nested types, never top-level types.
646685

647686
**Q:** What's the key difference between a static nested class and an inner class?
648-
**A:** A static nested class cannot access non-static members of the outer class and doesn't need an outer instance to be created, while an inner class (non-static nested) can access all outer class members and requires an outer instance.
687+
**A:** A static nested class cannot access non-static members of the outer class and doesn't need an outer instance to be created, while an inner class (non-static nested) can access all outer class members and requires an outer instance. Enums, records, and classes inside interfaces are always implicitly static.

0 commit comments

Comments
 (0)