🕵️ HackingReopening im Loft: Was ist neu da oben? - Wien - Kurier(17.09.2026 um 05:14 Uhr)
🕵️ HackingTangerhütte: Hacker greifen Verwaltung an - radio SAW(17.09.2026 um 06:32 Uhr)
🕵️ HackingReopening im Loft: Was ist neu da oben? - Wien - Kurier(17.09.2026 um 05:14 Uhr)
🕵️ HackingTangerhütte: Hacker greifen Verwaltung an - radio SAW(17.09.2026 um 06:32 Uhr)
🔧 Programmierung 🕛 vor 3 Monaten 2 Min Lesezeit
0

Why in older Java non-static inner classes could not have static members?

↗ Quelle (dev.to)
🗣️ Stimme:

Before Java 16

Let's understand this with an example




CODE
class Outer {
class Inner {
// Outer outer;
static int count = 5; // Not allowed
}
}






Now let's look at the main method:




CODE
Outer o1 = new Outer();
Outer o2 = new Outer();
Inner i1 = o1.new Inner();
Inner i2 = o2.new Inner()






If i1.count = 5, then what should i2.count be?

The confusion is that count could be associated with o1, or with o2, or with both of them.



Now consider these possibilities:




  • If every Outer instance (o1 and o2) has its own separate count, then it is no longer truly static, because static members are supposed to be shared.

  • If there is only one count shared between both o1 and o2, then the Inner class doesn't really depend on a specific Outer object, which contradicts the nature of a non-static inner class.



To avoid this confusion, Java decided that non-static inner classes cannot contain static fields or static methods.



How is an Inner Class Related to the Outer Class?

-->Every object of a non-static inner class contains a hidden reference to the outer class object that created it.

Conceptually, it looks something like this




CODE
class Inner {
Outer outer; // hidden reference
}






In the earlier example, Java automatically maintains this reference internally, so you don't need to write it yourself.



One Exception: static final

Before Java 16, Java allowed static final constants inside non-static inner classes:




CODE
class Outer {
class Inner {
static final int COUNT = 5; // Allowed
static int count = 0; // Not allowed
}
}






This was allowed because static final primitive constants are compile-time constants. They don't behave like regular static variables and are often inlined directly into the bytecode by the compiler.

Therefore, before Java 16, a non-static inner class could contain static final compile-time constants, but it could not contain ordinary static fields or static methods.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
CVE-2022-44251 | TOTOLINK NR1800X 9.1.0u.6279_B20210910 setUssd ussd command injection (EUVD-2022-47200)
2 Quellen
CVE-2022-44253 | TOTOLINK LR350 9.3.5u.6369_B20220309 setDiagnosisCfg via improper authentication (EUVD-2022-47202)
1 Quelle
„Gerät finden“ auf Android kann sich jetzt auf Zuruf merken, wo ihr Gegenstände abgelegt habt
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Why in older Java non-static inner classes could not have static members?

Thematisch verwandte Begriffe: older, Java, nonstatic, inner · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...