<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam Langmesser on Medium]]></title>
        <description><![CDATA[Stories by Adam Langmesser on Medium]]></description>
        <link>https://medium.com/@adam.langmesser?source=rss-39fab8230bed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*aoUKiJt3k0Ytjn87</url>
            <title>Stories by Adam Langmesser on Medium</title>
            <link>https://medium.com/@adam.langmesser?source=rss-39fab8230bed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:06:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adam.langmesser/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Serialization for Object cloning in Java]]></title>
            <link>https://medium.com/@adam.langmesser/using-serialization-for-object-cloning-in-java-028a2389a18e?source=rss-39fab8230bed------2</link>
            <guid isPermaLink="false">https://medium.com/p/028a2389a18e</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[cloneable]]></category>
            <category><![CDATA[java-serialization]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Adam Langmesser]]></dc:creator>
            <pubDate>Mon, 04 May 2026 14:04:09 GMT</pubDate>
            <atom:updated>2026-05-04T14:04:09.773Z</atom:updated>
            <content:encoded><![CDATA[<p>In Java, cloning is usually done by implementing the Cloneable marker interface and overriding the clone() method inherited from Object.</p><p>The thing that we have to take into consideration is the fact of the shallow/deep copy problem. If an instance that we want to clone has fields that are references to other objects, then if we want to have truly separate copy of the object we are cloning we have to remember to implement cloning of the referenced objects.</p><p><em>I am going to use lombok in my examples.</em></p><p><strong>Shallow clone example:</strong></p><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class Cat implements Cloneable {<br>    <br>    private String name;<br>    private LocalDate birth;<br>    private CatFood favouriteFood;<br><br>    // shallow clone - code autogenerated by IntelliJ IDEA, <br>    // after implementing the Cloneable interface<br>    @Override<br>    public Cat clone() {<br>        try {<br>            Cat clone = (Cat) super.clone();<br>            return clone;<br>        } catch (CloneNotSupportedException e) {<br>            throw new AssertionError();<br>        }<br>    }<br><br>}</pre><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class CatFood {<br><br>    private String brand;<br>    private double price;<br><br>}</pre><pre>class Main {<br><br>    static void main() {<br>        CatFood tuna = new CatFood(<br>                &quot;Tuna Company&quot;,<br>                200<br>        );<br><br>        Cat marcel = new Cat(<br>                &quot;Marcel&quot;,<br>                LocalDate.of(2018, 3, 26),<br>                tuna<br>        );<br><br>        Cat marcelCopy = marcel.clone();<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>        System.out.println();<br><br>        tuna.setPrice(1000);<br>        tuna.setBrand(&quot;New tuna brand&quot;);<br>        marcel.setName(&quot;Changed marcel name&quot;);<br>        marcel.setBirth(LocalDate.now());<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>    }<br>}</pre><pre>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br><br>Cat(name=Changed marcel name, birth=2026-05-04, favouriteFood=CatFood(brand=New tuna brand, price=1000.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=New tuna brand, price=1000.0))</pre><p>The default implementation provided by Object.clone() (we call it via super.clone()) creates a shallow copy. It copies primitive values directly and copies references to other objects, but it does not clone the objects behind those references.</p><p>As you can see, the shallow copy kept references to the original CatFoodIntacne. Change of price and brand of the tuna object affected both marcel and marcelCopy.</p><p>Changing marcel&#39;s name and birth does not affect marcelCopy, because these fields are reassigned only in the original Cat object. Also, String and LocalDate are immutable, so sharing references to them is usually safe.</p><p><strong>Example of deep copy:</strong></p><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class Cat implements Cloneable {<br><br>    private String name;<br>    private LocalDate birth;<br>    private CatFood favouriteFood;<br><br>    // deep clone<br>    @Override<br>    public Cat clone() {<br>        try {<br>            Cat clone = (Cat) super.clone();<br><br>            CatFood clonedFood = this.favouriteFood.clone();<br>            clone.setFavouriteFood(clonedFood);<br><br>            return clone;<br>        } catch (CloneNotSupportedException e) {<br>            throw new AssertionError();<br>        }<br>    }<br><br>}</pre><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class CatFood implements Cloneable {<br>    private String brand;<br>    private double price;<br><br>    @Override<br>    public CatFood clone() {<br>        try {<br>            CatFood clone = (CatFood) super.clone();<br>            return clone;<br>        } catch (CloneNotSupportedException e) {<br>            throw new AssertionError();<br>        }<br>    }<br>}</pre><pre>class Main {<br>    // no changes!<br>    static void main() {<br>        CatFood tuna = new CatFood(<br>                &quot;Tuna Company&quot;,<br>                200<br>        );<br><br>        Cat marcel = new Cat(<br>                &quot;Marcel&quot;,<br>                LocalDate.of(2018, 3, 26),<br>                tuna<br>        );<br><br>        Cat marcelCopy = marcel.clone();<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>        System.out.println();<br><br>        tuna.setPrice(1000);<br>        tuna.setBrand(&quot;New tuna brand&quot;);<br>        marcel.setName(&quot;Changed marcel name&quot;);<br>        marcel.setBirth(LocalDate.now());<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>    }<br>}</pre><pre>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br><br>Cat(name=Changed marcel name, birth=2026-05-04, favouriteFood=CatFood(brand=New tuna brand, price=1000.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))</pre><p>Now the clone() method of the Cat class creates a deep copy. If your goal is to have a truly independent copy of an object, you must make sure that all mutable objects referenced by the original object are copied as well.</p><p>This process is error-prone, especially when another programmer adds a new mutable reference field, such as Owner, Address, or List&lt;CatToy&gt;, and forgets to update the cloning logic.</p><p>But, there is a trick that can resolve that issue.</p><p><strong>Deep Cloning Objects via Serialization in Java</strong></p><p>In a broad sense, serialization means converting an object into a format that can be stored or transferred, and deserialization means reconstructing an object from that format. In this example, I use Java’s built-in object serialization mechanism, based on ObjectOutputStream and ObjectInputStream.</p><p>Deep clone via serialization example:</p><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class Cat implements Cloneable, Serializable {<br><br>    private String name;<br>    private LocalDate birth;<br>    private CatFood favouriteFood;<br><br>    // deep clone via serialization<br>    @Override<br>    public Cat clone() throws CloneNotSupportedException {<br>        try {<br>            ByteArrayOutputStream bout = new ByteArrayOutputStream();<br><br>            try (ObjectOutputStream out = new ObjectOutputStream(bout)) {<br>                out.writeObject(this);<br>            }<br>            <br>            try (ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());<br>                 ObjectInputStream in = new ObjectInputStream(bin)) {<br><br>                return (Cat) in.readObject();<br>            }<br><br>        } catch (IOException | ClassNotFoundException e) {<br>            CloneNotSupportedException e2 = new CloneNotSupportedException();<br>            e2.initCause(e);<br>            throw e2;<br>        }<br>    }<br><br>}</pre><pre>@AllArgsConstructor<br>@Getter<br>@Setter<br>@ToString<br>class CatFood implements Serializable {<br><br>    private String brand;<br>    private double price;<br><br>}</pre><pre>class Main {<br>    // added throws clause<br>    static void main() throws CloneNotSupportedException {<br>        CatFood tuna = new CatFood(<br>                &quot;Tuna Company&quot;,<br>                200<br>        );<br><br>        Cat marcel = new Cat(<br>                &quot;Marcel&quot;,<br>                LocalDate.of(2018, 3, 26),<br>                tuna<br>        );<br><br>        Cat marcelCopy = marcel.clone();<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>        System.out.println();<br><br>        tuna.setPrice(1000);<br>        tuna.setBrand(&quot;New tuna brand&quot;);<br>        marcel.setName(&quot;Changed marcel name&quot;);<br>        marcel.setBirth(LocalDate.now());<br><br>        System.out.println(marcel);<br>        System.out.println(marcelCopy);<br>    }<br>}</pre><pre>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))<br><br>Cat(name=Changed marcel name, birth=2026-05-04, favouriteFood=CatFood(brand=New tuna brand, price=1000.0))<br>Cat(name=Marcel, birth=2018-03-26, favouriteFood=CatFood(brand=Tuna Company, price=200.0))</pre><p>The object being cloned must implement Serializable. Also, every non-transient object reachable from it must be serializable as well. Otherwise, serialization will fail at runtime with a NotSerializableException.</p><p>Serialization-based cloning is convenient, but I would treat it mostly as an educational technique, unusable in an enterprise solution. Explicit copying is usually faster, easier to control, and easier to understand for other developers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=028a2389a18e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Break with labeled statement in Java]]></title>
            <link>https://medium.com/@adam.langmesser/break-with-labeled-statement-in-java-815d2b4a7a62?source=rss-39fab8230bed------2</link>
            <guid isPermaLink="false">https://medium.com/p/815d2b4a7a62</guid>
            <category><![CDATA[break-statement]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java-core-concepts]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Adam Langmesser]]></dc:creator>
            <pubDate>Sun, 03 May 2026 11:03:21 GMT</pubDate>
            <atom:updated>2026-05-03T11:07:22.342Z</atom:updated>
            <content:encoded><![CDATA[<p>If you used Java or any modern programming language you probably used break statement. In this article I want to show you a break statement used in a looptogether with a so called label or labeled statement.</p><p>Typically you can use break in a loop like that:</p><pre>for (int i = 1; i &lt;= 3; i++) {<br>    for (int j = 1; j &lt;= 3; j++) {<br>        System.out.println(&quot;i = &quot; + i + &quot; j = &quot; + j);<br>        if (j == 2) break; // omit every iteration with j &gt; 2<br>    }<br>    System.out.println();<br>}</pre><p>Above code prints to the console:</p><pre>i = 1 j = 1<br>i = 1 j = 2<br><br>i = 2 j = 1<br>i = 2 j = 2<br><br>i = 3 j = 1<br>i = 3 j = 2</pre><p>So a break statement on it’s own in a loop allows program to leave the loop body in which it was used and cancled all leftover iterations of that loop.</p><p>But in Java, there is a way to enhance this capability — labeled statement</p><pre>my_annotation:<br>for (int i = 1; i &lt;= 3; i++) {<br>    for (int j = 1; j &lt;= 3; j++) {<br>        System.out.println(&quot;i = &quot; + i + &quot; j = &quot; + j);<br>        if (j == 2) break my_annotation;<br>    }<br>    System.out.println();<br>}</pre><p>Here I’ve put a labeled statement with a name my_annotation. Now the output looks like that:</p><pre>i = 1 j = 1<br>i = 1 j = 2</pre><p>As you can see, a labeled break allows us to exit a labeled outer loop directly from within an inner loop.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=815d2b4a7a62" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>