<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mani</title>
    <description>The latest articles on DEV Community by Mani (@itsmemanikr).</description>
    <link>https://dev.to/itsmemanikr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3981450%2F6640c9cf-52be-4c2e-9fd7-1bca741cc60c.webp</url>
      <title>DEV Community: Mani</title>
      <link>https://dev.to/itsmemanikr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsmemanikr"/>
    <language>en</language>
    <item>
      <title>Why in older Java non-static inner classes could not have static members?</title>
      <dc:creator>Mani</dc:creator>
      <pubDate>Fri, 12 Jun 2026 14:57:28 +0000</pubDate>
      <link>https://dev.to/itsmemanikr/why-in-older-java-non-static-inner-classes-could-not-have-static-members-4a56</link>
      <guid>https://dev.to/itsmemanikr/why-in-older-java-non-static-inner-classes-could-not-have-static-members-4a56</guid>
      <description>&lt;p&gt;&lt;strong&gt;Before Java 16&lt;/strong&gt;&lt;br&gt;
Let's understand this with an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Outer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Inner&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
         &lt;span class="c1"&gt;// Outer outer;&lt;/span&gt;
         &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Not allowed&lt;/span&gt;
     &lt;span class="o"&gt;}&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's look at the main method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Outer&lt;/span&gt; &lt;span class="n"&gt;o1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Outer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Outer&lt;/span&gt; &lt;span class="n"&gt;o2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Outer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Inner&lt;/span&gt; &lt;span class="n"&gt;i1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Inner&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Inner&lt;/span&gt; &lt;span class="n"&gt;i2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Inner&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If i1.count = 5, then what should i2.count be?&lt;br&gt;
The confusion is that count could be associated with o1, or with o2, or with both of them.&lt;/p&gt;

&lt;p&gt;Now consider these possibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;To avoid this confusion, Java decided that non-static inner classes cannot contain static fields or static methods.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How is an Inner Class Related to the Outer Class?&lt;/strong&gt;&lt;br&gt;
--&amp;gt;Every object of a non-static inner class contains a hidden reference to the outer class object that created it.&lt;br&gt;
Conceptually, it looks something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Inner&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nc"&gt;Outer&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// hidden reference&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the earlier example, Java automatically maintains this reference internally, so you don't need to write it yourself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One Exception&lt;/strong&gt;: static final&lt;br&gt;
Before Java 16, Java allowed static final constants inside non-static inner classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Outer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Inner&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;COUNT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Allowed&lt;/span&gt;
       &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Not allowed&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
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.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>java</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
