<?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: EL HADDAD Mohamed</title>
    <description>The latest articles on DEV Community by EL HADDAD Mohamed (@haddad_404).</description>
    <link>https://dev.to/haddad_404</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4073699%2F9f1b4690-e249-4d98-9dd5-867268ced464.png</url>
      <title>DEV Community: EL HADDAD Mohamed</title>
      <link>https://dev.to/haddad_404</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haddad_404"/>
    <language>en</language>
    <item>
      <title>2 Tricky Java Quirks I Discovered While Preparing for the OCP Java SE 21 Exam</title>
      <dc:creator>EL HADDAD Mohamed</dc:creator>
      <pubDate>Tue, 11 Aug 2026 20:29:35 +0000</pubDate>
      <link>https://dev.to/haddad_404/2-tricky-java-quirks-i-discovered-while-preparing-for-the-ocp-java-se-21-exam-1lmf</link>
      <guid>https://dev.to/haddad_404/2-tricky-java-quirks-i-discovered-while-preparing-for-the-ocp-java-se-21-exam-1lmf</guid>
      <description>&lt;p&gt;While diving deep into my OCP Java SE 21 preparation, I stumbled upon two fascinating edge cases that are easy to miss during standard application development.&lt;/p&gt;

&lt;p&gt;Here is what happened when I experimented with Records and the &lt;code&gt;super&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1-You Can't Name Record Components Anything You Want&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Java Records, the compiler automatically generates accessor methods using the exact name of each record component. I decided to test what happens if I name a component &lt;code&gt;toString&lt;/code&gt;:&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="c1"&gt;// ❌ Compilation Error!&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;toString&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;&lt;strong&gt;Why does this fail?&lt;/strong&gt;&lt;br&gt;
Java explicitly prohibits naming record components after zero-parameter methods in &lt;code&gt;java.lang.Object&lt;/code&gt; (such as &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;hashCode&lt;/code&gt;, &lt;code&gt;equals&lt;/code&gt;, &lt;code&gt;getClass&lt;/code&gt;, &lt;code&gt;clone&lt;/code&gt;, &lt;code&gt;finalize&lt;/code&gt;, &lt;code&gt;notify&lt;/code&gt;, &lt;code&gt;notifyAll&lt;/code&gt;, &lt;code&gt;wait&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Because a record component named &lt;code&gt;toString&lt;/code&gt; would require generating an accessor method &lt;code&gt;toString()&lt;/code&gt;, it creates a naming collision with the inherited &lt;code&gt;Object.toString()&lt;/code&gt; method contract, triggering a compilation error immediately.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. Why &lt;code&gt;super.equals(this)&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To test how Java handles instance references in memory, I ran this test inside a standard class:&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReferenceTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ReferenceTest&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;evaluate&lt;/span&gt;&lt;span class="o"&gt;();&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;&lt;strong&gt;Why does it return &lt;code&gt;true&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;super&lt;/code&gt; is not a distinct object in memory. It is simply a keyword that refers to the &lt;strong&gt;current instance&lt;/strong&gt; (&lt;code&gt;this&lt;/code&gt;), while instructing the compiler to bypass overridden methods and use the superclass implementation instead.&lt;/p&gt;

&lt;p&gt;When calling &lt;code&gt;super.equals(this)&lt;/code&gt;, it invokes &lt;code&gt;Object.equals(Object obj)&lt;/code&gt;. The default implementation in &lt;code&gt;Object&lt;/code&gt; performs a reference check (&lt;code&gt;this == obj&lt;/code&gt;). Since &lt;code&gt;super&lt;/code&gt; and &lt;code&gt;this&lt;/code&gt; point to the exact same object reference, the result evaluates to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
