<div dir="ltr"><p>I am writing to propose a language enhancement regarding the handling of static member access in Java.</p>
<h2><strong>Issue</strong></h2>
<p>Java currently permits static fields and methods to be accessed through object references, despite static members belonging strictly to the class. This behavior is often misleading and can create confusion, especially in large codebases or among less-experienced developers.</p>
<p>Example:</p>
<pre class="gmail-overflow-visible!"><div class="gmail-contain-inline-size gmail-rounded-2xl gmail-relative gmail-bg-token-sidebar-surface-primary"><div class="gmail-sticky gmail-top-9"><div class="gmail-absolute end-0 gmail-bottom-0 gmail-flex gmail-h-9 gmail-items-center gmail-pe-2"><div class="gmail-bg-token-bg-elevated-secondary gmail-text-token-text-secondary gmail-flex gmail-items-center gmail-gap-4 gmail-rounded-sm gmail-px-2 gmail-font-sans gmail-text-xs"></div></div></div><div class="gmail-overflow-y-auto gmail-p-4" dir="ltr"><code class="gmail-whitespace-pre! gmail-language-java"><span class="gmail-hljs-type">MyClass</span> <span class="gmail-hljs-variable">obj</span> <span class="gmail-hljs-operator">=</span> <span class="gmail-hljs-keyword">new</span> <span class="gmail-hljs-title gmail-class_">MyClass</span>();
obj.staticMethod(); <span class="gmail-hljs-comment">// Currently allowed, but confusing</span>
</code></div></div></pre>
<h2><strong>Proposed Enhancement</strong></h2>
<p>I request consideration of a change that <strong>disallows access to static members via object references</strong>, enforcing access exclusively through the class name. This would convert the current warning into a <strong>compile-time error</strong>.</p>
<h2><strong>Rationale</strong></h2>
<ul>
<li>
<p>Prevents misconceptions about instance vs. class-level behavior</p>
</li>
<li>
<p>Improves code clarity and consistency</p>
</li>
<li>
<p>Reduces maintenance complexity in enterprise applications</p>
</li>
<li>
<p>Encourages best practices already recommended by the community</p></li></ul>
<h2><strong>Suggested Requirements</strong></h2>
<ol>
<li>
<p>Compiler should produce an error when static members are accessed through object references.</p>
</li>
<li>
<p>Error message should explicitly guide developers to use class-based access.</p>
</li>
<li>
<p>Rule should apply to static fields, static methods, and static nested types.</p>
</li>
<li>
<p>Optionally, provide a compiler flag for backward compatibility during migration.</p>
</li>
</ol>
<h2><strong>Conclusion</strong></h2>
<p>Restricting object-based access to static members would strengthen language clarity and help eliminate a common source of misunderstanding. I kindly request your consideration of this enhancement for future Java releases.</p>
<p>Thank you for your time and continued work on the Java platform.</p>
<p>Sincerely,<br><b>Kamlesh Kohli<br><br></b></p></div>