<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Java on Aamer Paul</title>
    <link>https://aamernabi.github.io/tags/java/</link>
    <description>Recent content in Java on Aamer Paul</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Sat, 02 Aug 2025 08:53:20 +0530</lastBuildDate>
    <atom:link href="https://aamernabi.github.io/tags/java/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Strategy Pattern in Action</title>
      <link>https://aamernabi.github.io/posts/strategy-design-pattern-with-example/</link>
      <pubDate>Sat, 02 Aug 2025 08:53:20 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/strategy-design-pattern-with-example/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Strategy Pattern&lt;/strong&gt; is a behavioral design pattern that enables selecting an algorithm’s behavior at runtime.&lt;/p&gt;&#xA;&lt;p&gt;It&amp;rsquo;s widely used and very flexible, perfect for cases where you want to avoid complex &lt;code&gt;if-else&lt;/code&gt; or &lt;code&gt;switch&lt;/code&gt; statements.&lt;/p&gt;&#xA;&lt;h3 id=&#34;use-cases&#34;&gt;Use cases&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;You want to replace conditionals (&lt;code&gt;if-else&lt;/code&gt; or &lt;code&gt;switch&lt;/code&gt;) with &lt;strong&gt;polymorphism&lt;/strong&gt;.&lt;/li&gt;&#xA;&lt;li&gt;You need to switch behavior at runtime or make it configurable. Example: User selecting payment option&lt;/li&gt;&#xA;&lt;li&gt;You want to define multiple ways of doing something (e.g., sorting, compression, payment).&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;core-concepts&#34;&gt;Core Concepts&lt;/h3&gt;&#xA;&lt;p&gt;The pattern consists of three main components:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Builder Design Pattern</title>
      <link>https://aamernabi.github.io/posts/builder-design-pattern/</link>
      <pubDate>Mon, 28 Jul 2025 18:30:20 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/builder-design-pattern/</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Builder Design Pattern&lt;/strong&gt; is a creational design pattern that lets you construct complex objects step by step. Using Builder pattern you can use same construction process to can create different representations.&lt;/p&gt;&#xA;&lt;h3 id=&#34;use-case&#34;&gt;Use case&lt;/h3&gt;&#xA;&lt;p&gt;Use the Builder Pattern when:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;An object has many &lt;strong&gt;optional fields or configurations&lt;/strong&gt;.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;You want to avoid constructor telescoping (i.e., too many overloaded constructors).&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;You need to create &lt;strong&gt;immutable objects&lt;/strong&gt; step by step.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;User&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; String firstName;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; String lastName;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; age;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; String email;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; String phone;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;User&lt;/span&gt;(String firstName, String lastName) { ... }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;User&lt;/span&gt;(String firstName, String lastName, &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; age) { ... }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;User&lt;/span&gt;(String firstName, String lastName, &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; age, String email) { ... }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// ...&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In above example, you can see &lt;code&gt;User&lt;/code&gt; class is hard to maintain and read.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Factory and Abstract Factory Design Pattern</title>
      <link>https://aamernabi.github.io/posts/factory-design-pattern/</link>
      <pubDate>Sun, 27 Jul 2025 10:51:59 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/factory-design-pattern/</guid>
      <description>&lt;h2 id=&#34;factory-design-pattern&#34;&gt;Factory Design Pattern&lt;/h2&gt;&#xA;&lt;p&gt;&lt;strong&gt;Factory Pattern&lt;/strong&gt; is &lt;strong&gt;creational pattern&lt;/strong&gt; that delegate the instantiation of objects to a &lt;strong&gt;separate method or class&lt;/strong&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The Factory design pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that&amp;rsquo;ll be created. This pattern helps to decouple the client code from the concrete classes it needs to instantiate.&lt;/p&gt;&#xA;&lt;h3 id=&#34;core-components&#34;&gt;Core Components&lt;/h3&gt;&#xA;&lt;p&gt;The Factory pattern has three main components:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Singleton Design Pattern</title>
      <link>https://aamernabi.github.io/posts/singleton-design-pattern/</link>
      <pubDate>Sat, 26 Jul 2025 11:02:04 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/singleton-design-pattern/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Singleton design pattern&lt;/strong&gt; is a creational design pattern that restricts instantiation of a class ensuring class has only &lt;strong&gt;one instance&lt;/strong&gt; and provide a &lt;strong&gt;global point&lt;/strong&gt; of access to it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;common-implementations&#34;&gt;Common Implementations&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Lazy Initialization&lt;/strong&gt;: ensures instance is created only when it is first requested.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Eager Initialization&lt;/strong&gt;: instance is created when the class is loaded.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Thread-Safe Implementations&lt;/strong&gt;: ensures the singleton works correctly in multi-threaded environments, such as using &lt;code&gt;synchronized&lt;/code&gt; methods or double-checked locking.&#xA;(though double-checked locking has nuances and is sometimes considered an anti-pattern in modern Java)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;use-cases&#34;&gt;Use Cases&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Logging&lt;/li&gt;&#xA;&lt;li&gt;Configuration settings&lt;/li&gt;&#xA;&lt;li&gt;Thread pools&lt;/li&gt;&#xA;&lt;li&gt;Caches&lt;/li&gt;&#xA;&lt;li&gt;Device drivers&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;&#xA;&lt;h4 id=&#34;eager-initialization&#34;&gt;Eager initialization&lt;/h4&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;final&lt;/span&gt; Singleton instance &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; Singleton();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;EagerInitializedSingleton&lt;/span&gt;(){}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; Singleton &lt;span style=&#34;color:#a6e22e&#34;&gt;getInstance&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; instance;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;Useful if your singleton class is not using a lot of resources.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;using-synchronized-method-lazy-initialization&#34;&gt;Using &lt;code&gt;synchronized&lt;/code&gt; method (Lazy initialization)&lt;/h4&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// Volatile ensures visibility and prevents reordering&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;volatile&lt;/span&gt; Singleton instance;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// Thread-safe with method-level synchronization&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;synchronized&lt;/span&gt; Singleton &lt;span style=&#34;color:#a6e22e&#34;&gt;getInstance&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (instance &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            instance &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; Singleton();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; instance;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;example&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        System.&lt;span style=&#34;color:#a6e22e&#34;&gt;out&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;println&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello from Singleton!, (using synchronized method)&amp;#34;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;Lazily initialization the instance.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;synchronized&lt;/code&gt; ensures that only one thread can enter the method at a time.&lt;/li&gt;&#xA;&lt;li&gt;It ensures lazy initialization — Singleton instance is created only when needed.&lt;/li&gt;&#xA;&lt;li&gt;Performance overhead: Every call to &lt;code&gt;getInstance()&lt;/code&gt; acquires a lock, even after the instance has been initialized. This is unnecessary and can be expensive under heavy load.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;double-checked-locking-lazy-initialization&#34;&gt;Double-Checked Locking (Lazy initialization)&lt;/h4&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;volatile&lt;/span&gt; Singleton instance;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt;() {}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; Singleton &lt;span style=&#34;color:#a6e22e&#34;&gt;getInstance&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (instance &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;synchronized&lt;/span&gt; (Singleton.&lt;span style=&#34;color:#a6e22e&#34;&gt;class&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (instance &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) { &lt;span style=&#34;color:#75715e&#34;&gt;// &amp;lt;- Double-check&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    instance &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; Singleton();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; instance;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;example&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        System.&lt;span style=&#34;color:#a6e22e&#34;&gt;out&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;println&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello from Singleton!, (double-check)&amp;#34;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;Lazily initialization the instance.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;double check&lt;/strong&gt; aims to minimize the overhead of synchronization.&lt;/li&gt;&#xA;&lt;li&gt;Complex&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;bill-pugh-inner-class---recommended-approach&#34;&gt;Bill Pugh (Inner Class) - Recommended Approach&lt;/h4&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Singleton&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#75715e&#34;&gt;// Prevent reflection attack&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (SingletonHelper.&lt;span style=&#34;color:#a6e22e&#34;&gt;INSTANCE&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;throw&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; RuntimeException(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Use getInstance() method to get the single instance of this class&amp;#34;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;SingletonHelper&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;final&lt;/span&gt; Singleton INSTANCE &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; Singleton();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; Singleton &lt;span style=&#34;color:#a6e22e&#34;&gt;getInstance&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; SingletonHelper.&lt;span style=&#34;color:#a6e22e&#34;&gt;INSTANCE&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;example&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        System.&lt;span style=&#34;color:#a6e22e&#34;&gt;out&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;println&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello from Singleton!, (Bill Pugh)&amp;#34;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Static inner helper class&lt;/strong&gt; is loaded only when &lt;code&gt;getInstance()&lt;/code&gt; is called.&lt;/li&gt;&#xA;&lt;li&gt;Class loading is thread-safe.&lt;/li&gt;&#xA;&lt;li&gt;No need for synchronized, and &lt;strong&gt;lazy initialization&lt;/strong&gt; is achieved.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;drawbacks&#34;&gt;Drawbacks&lt;/h3&gt;&#xA;&lt;p&gt;Despite its utility, the Singleton pattern is sometimes criticized as an anti-pattern because it can:&lt;/p&gt;</description>
    </item>
    <item>
      <title>What&#39;s new in Spring Boot 4.0? A Look Ahead</title>
      <link>https://aamernabi.github.io/posts/spring-boot-4.0/</link>
      <pubDate>Thu, 24 Jul 2025 10:24:00 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/spring-boot-4.0/</guid>
      <description>&lt;p&gt;The Spring ecosystem is constantly evolving to provide developers with the best tools for building modern, robust applications. With the upcoming release of Spring Framework 7.0, the community is eagerly anticipating the next major iteration of Spring Boot: version 4.0.&lt;/p&gt;&#xA;&lt;p&gt;Spring Boot 4.0 is a major release of the Spring Boot framework, introducing significant enhancements for developers.&lt;/p&gt;&#xA;&lt;p&gt;Key features and improvements in Spring Boot 4 includes:&lt;/p&gt;&#xA;&lt;h2 id=&#34;core-enhancements&#34;&gt;Core Enhancements&lt;/h2&gt;&#xA;&lt;p&gt;Spring Boot 4.0 is built upon &lt;strong&gt;Spring Framework 7.0&lt;/strong&gt; which is major upcoming release of Spring Framework (projected for General Availability in November 2025).&lt;/p&gt;</description>
    </item>
    <item>
      <title>Controlling Inheritance with Java&#39;s Sealed Classes and Interfaces</title>
      <link>https://aamernabi.github.io/posts/java-sealed-interface/</link>
      <pubDate>Tue, 02 Jul 2024 16:25:32 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-sealed-interface/</guid>
      <description>&lt;p&gt;Introduced as a standard feature in Java 17, &lt;strong&gt;sealed classes&lt;/strong&gt; and &lt;strong&gt;sealed interfaces&lt;/strong&gt; give you precise control over your class hierarchies. By declaring a class or interface as &lt;code&gt;sealed&lt;/code&gt;, you can restrict which other classes or interfaces are allowed to extend or implement it.&lt;/p&gt;&#xA;&lt;p&gt;This feature is a powerful tool for domain modeling, enhancing security, and working with pattern matching in &lt;code&gt;switch&lt;/code&gt; expressions.&lt;/p&gt;&#xA;&lt;h2 id=&#34;what-are-sealed-classes&#34;&gt;What are Sealed Classes?&lt;/h2&gt;&#xA;&lt;p&gt;A &lt;strong&gt;sealed class&lt;/strong&gt; restricts which other classes can inherit from it. This provides better control over the class hierarchy, making your code more robust and maintainable.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Understanding Functional Interfaces in Java</title>
      <link>https://aamernabi.github.io/posts/java-functional-interface/</link>
      <pubDate>Tue, 26 Mar 2024 21:25:32 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-functional-interface/</guid>
      <description>&lt;p&gt;A &lt;strong&gt;functional interface&lt;/strong&gt; in Java is an interface that contains exactly one abstract method. Introduced in Java 8, they are the backbone of lambda expressions and the Stream API, enabling a more functional style of programming. They are also known as &lt;strong&gt;Single Abstract Method (SAM)&lt;/strong&gt; interfaces.&lt;/p&gt;&#xA;&lt;p&gt;To ensure an interface is a functional interface, you can use the &lt;code&gt;@FunctionalInterface&lt;/code&gt; annotation. The compiler will then trigger an error if the interface does not meet the requirements.&lt;/p&gt;</description>
    </item>
    <item>
      <title>A Deep Dive into the Java Stream API</title>
      <link>https://aamernabi.github.io/posts/deep-dive-into-java-stream-api/</link>
      <pubDate>Mon, 07 Jun 2021 18:12:23 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/deep-dive-into-java-stream-api/</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Stream API&lt;/strong&gt;, introduced in Java 8, is a powerful tool for processing sequences of elements. It provides a functional approach to working with collections and arrays, allowing for expressive and efficient data manipulation. Streams don&amp;rsquo;t store data; instead, they operate on a data source, such as a &lt;code&gt;Collection&lt;/code&gt; or an array, and enable aggregate operations.&lt;/p&gt;&#xA;&lt;h2 id=&#34;core-concepts&#34;&gt;Core Concepts&lt;/h2&gt;&#xA;&lt;p&gt;A stream pipeline consists of three parts:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;A source&lt;/strong&gt;: Where the stream originates from (e.g., a &lt;code&gt;List&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, or array).&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Zero or more intermediate operations&lt;/strong&gt;: These transform the stream into another stream. Examples include &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, and &lt;code&gt;sorted&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;A terminal operation&lt;/strong&gt;: This produces a result or a side-effect, and triggers the execution of the pipeline. Examples include &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;collect&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;One of the key features of streams is &lt;strong&gt;laziness&lt;/strong&gt;. Intermediate operations are not executed until a terminal operation is invoked. This allows the Stream API to optimize the execution of the pipeline.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Thread-Safe Counters in Java: A Deep Dive into AtomicInteger</title>
      <link>https://aamernabi.github.io/posts/thread-safe_counters_in_java_atomicinteger/</link>
      <pubDate>Tue, 19 Jan 2021 10:41:26 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/thread-safe_counters_in_java_atomicinteger/</guid>
      <description>&lt;p&gt;When building multithreaded applications in Java, ensuring data integrity is crucial. A common challenge arises when multiple threads need to update a shared counter. Without proper synchronization, you can encounter race conditions, leading to incorrect results. This is where &lt;code&gt;AtomicInteger&lt;/code&gt; comes to the rescue.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;AtomicInteger&lt;/code&gt; is a class from the &lt;code&gt;java.util.concurrent.atomic&lt;/code&gt; package that provides a thread-safe way to perform atomic operations on an integer value. Unlike traditional locking mechanisms (e.g., &lt;code&gt;synchronized&lt;/code&gt; blocks), &lt;code&gt;AtomicInteger&lt;/code&gt; uses low-level hardware instructions to achieve thread safety with higher performance.&lt;/p&gt;</description>
    </item>
    <item>
      <title>A Deep Dive into Java&#39;s BlockingQueue</title>
      <link>https://aamernabi.github.io/posts/java-blocking-queue/</link>
      <pubDate>Mon, 27 Jul 2020 20:09:10 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-blocking-queue/</guid>
      <description>&lt;p&gt;A &lt;code&gt;BlockingQueue&lt;/code&gt; in Java is a thread-safe queue that simplifies concurrency management by providing blocking operations. It&amp;rsquo;s a crucial tool for building robust multi-threaded applications, especially for implementing producer-consumer scenarios.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-do-we-need-blockingqueue&#34;&gt;Why Do We Need &lt;code&gt;BlockingQueue&lt;/code&gt;?&lt;/h2&gt;&#xA;&lt;p&gt;In multi-threaded applications, it&amp;rsquo;s common to have a scenario where one or more threads (Producers) are generating data, and one or more other threads (Consumers) are processing that data. This is known as the &lt;strong&gt;producer-consumer problem&lt;/strong&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Mastering Asynchronous Programming with Java&#39;s CompletableFuture</title>
      <link>https://aamernabi.github.io/posts/java-completable-future/</link>
      <pubDate>Mon, 27 Jul 2020 12:09:10 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-completable-future/</guid>
      <description>&lt;p&gt;Introduced in Java 8, &lt;code&gt;CompletableFuture&lt;/code&gt; is a powerful class for asynchronous programming. It extends the traditional &lt;code&gt;Future&lt;/code&gt; interface with a rich set of features for composing, combining, and handling asynchronous tasks, enabling you to write non-blocking, highly concurrent applications with ease.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-completablefuture&#34;&gt;Why &lt;code&gt;CompletableFuture&lt;/code&gt;?&lt;/h2&gt;&#xA;&lt;p&gt;Before &lt;code&gt;CompletableFuture&lt;/code&gt;, Java&amp;rsquo;s &lt;code&gt;Future&lt;/code&gt; was limited. You could start an asynchronous task, but you had to block your main thread using &lt;code&gt;get()&lt;/code&gt; to retrieve the result. This defeated the purpose of non-blocking code.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Understanding Java&#39;s hashCode() and equals() Contract</title>
      <link>https://aamernabi.github.io/posts/java-hashcode/</link>
      <pubDate>Sun, 26 Jul 2020 23:03:02 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-hashcode/</guid>
      <description>&lt;p&gt;In Java, the &lt;code&gt;hashCode()&lt;/code&gt; method, inherited from the &lt;code&gt;Object&lt;/code&gt; class, plays a crucial role in the efficient functioning of hash-based collections like &lt;code&gt;HashMap&lt;/code&gt;, &lt;code&gt;HashSet&lt;/code&gt;, and &lt;code&gt;Hashtable&lt;/code&gt;. It generates an integer hash code value for an object, which is used to determine the &amp;ldquo;bucket&amp;rdquo; where the object should be stored.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-hashcode-and-equals-contract&#34;&gt;The &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;equals()&lt;/code&gt; Contract&lt;/h2&gt;&#xA;&lt;p&gt;The most important concept to understand is the contract between &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;equals()&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;If two objects are equal according to the &lt;code&gt;equals(Object)&lt;/code&gt; method, then calling the &lt;code&gt;hashCode()&lt;/code&gt; method on each of the two objects must produce the same integer result.&lt;/li&gt;&#xA;&lt;li&gt;It is &lt;em&gt;not&lt;/em&gt; required that if two objects are unequal according to the &lt;code&gt;equals(Object)&lt;/code&gt; method, then calling the &lt;code&gt;hashCode()&lt;/code&gt; method on each of the two objects must produce distinct integer results. However, producing distinct hash codes for unequal objects may improve the performance of hash tables.&lt;/li&gt;&#xA;&lt;/ol&gt;&lt;/blockquote&gt;&#xA;&lt;p&gt;In simple terms: &lt;strong&gt;equal objects must have equal hash codes.&lt;/strong&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Understanding Fail-Fast vs. Fail-Safe Iterators in Java</title>
      <link>https://aamernabi.github.io/posts/java-fail-fast-vs-fail-safe-iterators/</link>
      <pubDate>Mon, 20 Jul 2020 15:44:00 +0530</pubDate>
      <guid>https://aamernabi.github.io/posts/java-fail-fast-vs-fail-safe-iterators/</guid>
      <description>&lt;p&gt;When working with collections in Java, you&amp;rsquo;ll often use iterators to traverse through their elements. However, not all iterators are created equal. Two common types you&amp;rsquo;ll encounter are &lt;strong&gt;fail-fast&lt;/strong&gt; and &lt;strong&gt;fail-safe&lt;/strong&gt; iterators. Understanding the difference between them is crucial for writing robust and error-free code.&lt;/p&gt;&#xA;&lt;h2 id=&#34;fail-fast-iterators&#34;&gt;Fail-Fast Iterators&lt;/h2&gt;&#xA;&lt;p&gt;Fail-fast iterators immediately throw a &lt;code&gt;ConcurrentModificationException&lt;/code&gt; if the collection is structurally modified (i.e., elements are added or removed) at any time after the iterator is created, except through the iterator&amp;rsquo;s own &lt;code&gt;remove()&lt;/code&gt; method.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
