<?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: Tin Trinh</title>
    <description>The latest articles on DEV Community by Tin Trinh (@htintrinh).</description>
    <link>https://dev.to/htintrinh</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%2F376495%2Fee601608-a817-45c7-9c58-c9eef6b916fd.jpg</url>
      <title>DEV Community: Tin Trinh</title>
      <link>https://dev.to/htintrinh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/htintrinh"/>
    <language>en</language>
    <item>
      <title>Write effective code for equals method</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Wed, 02 Sep 2020 00:51:31 +0000</pubDate>
      <link>https://dev.to/htintrinh/write-effective-code-for-equals-method-kg0</link>
      <guid>https://dev.to/htintrinh/write-effective-code-for-equals-method-kg0</guid>
      <description>&lt;h3&gt;
  
  
  Why do we need to override the equals method
&lt;/h3&gt;

&lt;p&gt;Q: When to override the &lt;code&gt;equals&lt;/code&gt; method? &lt;br&gt;
A: If the class not override it and the object identity is merely not enough for equality.&lt;/p&gt;

&lt;p&gt;A right implementation of &lt;code&gt;equals&lt;/code&gt; makes the instance object to serve as map keys or set elements with predictable and desirable behavior&lt;/p&gt;

&lt;p&gt;&lt;code&gt;equals&lt;/code&gt; method is characteristic: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reflexive: an object should equal itself.&lt;/li&gt;
&lt;li&gt;symmetric: if object A equals object B, object B should equal with object A as well&lt;/li&gt;
&lt;li&gt;transitive: if A equals B, B equals C, so A should equal C.&lt;/li&gt;
&lt;li&gt;consistency: the same result will return no matter how many times you run it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And one more important characteristic that only exists in the OOP world. &lt;br&gt;
&lt;em&gt;Lisov substitution principle&lt;/em&gt; says that any important property of a type should also hold for all its subtypes so that any method written for the type should work equally well on its subtypes.&lt;br&gt;
What does that mean?&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&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;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RED&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;a&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="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// should return true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Extend instantiable class and add a value component to it violate the transitive rule easily.&lt;br&gt;
Like &lt;code&gt;java.util.Timstamp&lt;/code&gt; extend from &lt;code&gt;java.util.Date&lt;/code&gt; and add nanoseconds field.&lt;/p&gt;

&lt;p&gt;One common mistake this that do the null checking and getClass() to compare in the equal method. That will make the equals method not able to have the Lisov substitution characteristic.&lt;/p&gt;

&lt;p&gt;Not necessary to null checking in the &lt;code&gt;equals&lt;/code&gt; method. Simply cast it to the object we want so we could have the accessor to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;High quality &lt;code&gt;equals&lt;/code&gt; method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the == operator to check if the argument is a reference to this object: Performance optimization&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;instanceof&lt;/code&gt; to check if the arg has the correct type. No need to do the null check.&lt;/li&gt;
&lt;li&gt;Cast argument to correct type.&lt;/li&gt;
&lt;li&gt;Check if a file of the arg matches the corresponding field: if &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt; field we need to call &lt;code&gt;Float.compare&lt;/code&gt; and &lt;code&gt;Double.compare&lt;/code&gt; the method, if the field is object filed call the &lt;code&gt;equals&lt;/code&gt; method recursively. To avoid the possibility of &lt;code&gt;NullPointerException&lt;/code&gt;, using &lt;code&gt;Objects.equals(Object, Object)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After writing the &lt;code&gt;equals&lt;/code&gt; method.&lt;br&gt;
Is it symmetric? Is it transitive? Is it consistent? &lt;/p&gt;

&lt;p&gt;Use the &lt;code&gt;AutoValue&lt;/code&gt; framework to write &lt;code&gt;hashCode&lt;/code&gt; and &lt;code&gt;equals&lt;/code&gt; more easily.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Effective Java: #7 Obsolete object and memory leak</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Sun, 02 Aug 2020 07:47:54 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-7-obsolete-object-and-memory-leak-n5o</link>
      <guid>https://dev.to/htintrinh/effective-java-7-obsolete-object-and-memory-leak-n5o</guid>
      <description>&lt;p&gt;Java is the Garbage Collector programing language. What does that mean to you?&lt;/p&gt;

&lt;p&gt;I mean that the language will take care of cleaning the unused objects in your program. If you come from a language like C/C++ you will feel that this is like a superpower because you don't have to manually clear the memory for the objects that you don't use anymore (the task that the developer keeps forgetting a lot).&lt;/p&gt;

&lt;p&gt;Java did the magic through its Garbage Collection algorithm. There are a lot of them currently but basically it will check for the reference of the currently executed program with the objects that exist in the heap. If there is any object that wasn't referenced by the program, it will clear those objects as it considers those objects as garbage.&lt;/p&gt;

&lt;p&gt;Let's consider this example, that I took from the book:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;PoolObjects&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;DEFAULT_POOL_SIZE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&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="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;PoolsObjects&lt;/span&gt;&lt;span class="o"&gt;()&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="na"&gt;pools&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;Object&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;DEFAULT_POOL_SIZE&lt;/span&gt;&lt;span class="o"&gt;];&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;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&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="na"&gt;ensurePoolSize&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="na"&gt;pools&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="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;++]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&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="na"&gt;size&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="o"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmptyPoolException&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;r&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="na"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;[--&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ensurePoolCapacity&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&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="na"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&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="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&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="na"&gt;pools&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOf&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="na"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;In the above example, there is a leaking memory point that is very subtle. As you can reason, as we decrease the size of the array, the element that has an index less than the size value will be inactive or should be discarded and clear by the GC. But actually it won't be because that object still gets referred by the &lt;code&gt;pools&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;One way to fix the problem is nulling out the element that hasn't been referred anymore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&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="na"&gt;size&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="o"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmptyPoolException&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;r&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="na"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;[--&lt;/span&gt;&lt;span class="n"&gt;size&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="na"&gt;pools&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Explicitly nulling out the element at size index.&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&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;I would like to conclude the blog with some commonplace that memory leak could happen.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The application that manages the memory by itself. The example above&lt;/li&gt;
&lt;li&gt;The application that uses caches. The object cached could stay in the memory forever.&lt;/li&gt;
&lt;li&gt;The listener or callback that registers but not dis-register.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>effectivejava</category>
      <category>memory</category>
    </item>
    <item>
      <title>Effective Java Part 5 - Prefer dependency injection to hardwiring resource</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Tue, 30 Jun 2020 14:22:24 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-part-5-prefer-dependency-injection-to-hardwiring-resource-26d8</link>
      <guid>https://dev.to/htintrinh/effective-java-part-5-prefer-dependency-injection-to-hardwiring-resource-26d8</guid>
      <description>&lt;p&gt;There are a ton of situations that you need to create an object that depends on the other objects. In those situations, using Singleton or static class is not appropriate. &lt;/p&gt;

&lt;p&gt;It's better to have the class constructor to allow to pass in the resource that needs to parameterized the constructed object instead of using setter to change its behavior that may cause the class hard to test and maintain as well as hard to track its status transformation.&lt;/p&gt;

&lt;p&gt;When writing the constructor, let's think about what we really depend on when we create the object. And pass those dependencies in the object is somewhat called dependency injection. &lt;/p&gt;

&lt;p&gt;Nowadays, there are lots of framework help to do dependency injection easily so we don't have to do it manually. But the idea of them is the same. By showing the interface of what we depend on in the constructor and satisfy those dependencies when initiating them.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Effective Java Part 4 - Enforce noninstantiability with a private constructor</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Thu, 25 Jun 2020 15:38:34 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-part-4-enforce-noninstantiability-with-a-private-constructor-104i</link>
      <guid>https://dev.to/htintrinh/effective-java-part-4-enforce-noninstantiability-with-a-private-constructor-104i</guid>
      <description>&lt;p&gt;When to use this?&lt;br&gt;
When you want to create a class to hold bunch of static fields or static method. And you don't want to create any instance of the class.&lt;/p&gt;

&lt;p&gt;To prevent making object from class we could mark the class as abstract but this would make the class looks like it mean to be inherited by other subclass and those subclass could be instantiated.&lt;/p&gt;

&lt;p&gt;So the best way to enforce the noninstantiability is create an private constructor for the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;A&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;Even better but not strictly required, an exception could be throw if the constructor will be called setting would help to prevent unexpected call to the constructor from inside of the class&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;AssertError&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;



</description>
      <category>java</category>
    </item>
    <item>
      <title>Effective Java Part3 - Singleton</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Thu, 25 Jun 2020 15:26:27 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-part3-singleton-1hbh</link>
      <guid>https://dev.to/htintrinh/effective-java-part3-singleton-1hbh</guid>
      <description>&lt;p&gt;Singleton in Java is a concept that describes that a class should have one and only one of the instantiated object. What does that mean? With one class there is no different object initiated. &lt;/p&gt;

&lt;p&gt;There is some way to implement that. &lt;br&gt;
First, we can use the static public instance filed in class.&lt;br&gt;
For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;A&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="kd"&gt;final&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;A&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;With the static final filed, we make a very clear statement about the class&lt;/p&gt;

&lt;p&gt;The second way to implement a Singleton is by using the static factory method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&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="no"&gt;A&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;A&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;By using this static factory method we could have some advantage over using the public static instance field.&lt;br&gt;
We could change not using Singleton anymore without changing the API of the class. We could also return the class that is a subtype of the return class.&lt;/p&gt;

</description>
      <category>java</category>
      <category>singleton</category>
    </item>
    <item>
      <title>Effective Java Programing Part 2 - Creating and destroying objects.</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Thu, 25 Jun 2020 00:45:10 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-programing-part-2-creating-and-destroying-objects-3mlp</link>
      <guid>https://dev.to/htintrinh/effective-java-programing-part-2-creating-and-destroying-objects-3mlp</guid>
      <description>&lt;p&gt;&lt;em&gt;Consider the static factory methods over the constructors&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One advantage of using factory methods over the constructors is that the factory method has a name. If the parameters that passed into the constructor is not helping to understand what object will be returned to the client code.&lt;/p&gt;

&lt;p&gt;The factory method with a well-chosen name will help the client code easier to read.&lt;/p&gt;

&lt;p&gt;The second advantage of using a factory method over the constructor is that it doesn't have to return a new object every time it was invoked. So this method is very helpful when we trying to create an object that will be used a lot or expensive to create. And that class will be called instance-controlled. Because the class controls the creation of its class. Those classes could be Singleton or Non-Instantiable. It allows immutable class can guarantee that no two equal instances exist. &lt;code&gt;a.equal(b)&lt;/code&gt; if and only if &lt;code&gt;a == b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The third advantage is that it could return any subtype of its return type. This will be a good practice to return an interface as return type and has the implementation-specific class hidden.&lt;/p&gt;

&lt;p&gt;With Java 8, the interface could have the static method. I didn't know about that or how that could help me to do anything? Why would I need a static method for an interface?&lt;/p&gt;

&lt;p&gt;In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDriver is the provider registration API, DriverManager.getConnection is the service access API, and Driver is the service provider interface.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Effective Java Programming Part1 - Some thoughts about effective programming</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Wed, 24 Jun 2020 18:51:10 +0000</pubDate>
      <link>https://dev.to/htintrinh/effective-java-programming-part1-some-thoughts-about-effective-programming-4n3d</link>
      <guid>https://dev.to/htintrinh/effective-java-programming-part1-some-thoughts-about-effective-programming-4n3d</guid>
      <description>&lt;p&gt;First rule first, code should be reused instead of copying. &lt;/p&gt;

&lt;p&gt;Learning the art of programing is like other disciplines, consist of learning the rule and then learning when to break them with good reason.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>@Bean and @Component. What is the different? Which one should use be used?</title>
      <dc:creator>Tin Trinh</dc:creator>
      <pubDate>Sat, 23 May 2020 05:24:06 +0000</pubDate>
      <link>https://dev.to/htintrinh/bean-and-component-what-is-the-different-which-one-should-use-be-used-3foe</link>
      <guid>https://dev.to/htintrinh/bean-and-component-what-is-the-different-which-one-should-use-be-used-3foe</guid>
      <description>&lt;p&gt;The most benefit of using a framework like Spring is auto-configuration. By only introducing the library or the module to the classpath, Spring could automatically scan it and create the necessary object and &lt;code&gt;@Autowrired&lt;/code&gt; it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Component&lt;/code&gt; is an annotation that annotates a &lt;em&gt;class&lt;/em&gt;. It tells Spring to use this class to create a bean if there is somewhere else depend on this class. The creation of the class is totally controlled by Spring. And only one bean created per class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&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;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;…&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;)&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="na"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&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;In this example, Spring will recognize the appearance of the &lt;code&gt;UserSerivc&lt;/code&gt;e class because it was marked with &lt;code&gt;@Component&lt;/code&gt; annotation. So when it tries to initiate the &lt;code&gt;UserController&lt;/code&gt; object, it knows that it needs a &lt;code&gt;UserService&lt;/code&gt; to put to &lt;code&gt;UserController&lt;/code&gt; constructor. And the creation of the &lt;code&gt;UserService&lt;/code&gt; will be controlled totally by Spring. It is a pretty declarative way to define dependency.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;@Component&lt;/code&gt; our life is so good. So why do we even need something like &lt;code&gt;@Bean&lt;/code&gt; annotation? When should we use it? First, &lt;code&gt;@Bean&lt;/code&gt; is an annotation that used for annotating the function (not a class) that will return an object of a class that will be registered as a bean object by Spring. &lt;/p&gt;

&lt;p&gt;You could use it in case you are using a third-party library when you don’t have access to the source code of the library. So you can’t just put &lt;code&gt;@Component&lt;/code&gt; annotation of the class that you to create a bean.&lt;br&gt;
You can combine with the &lt;code&gt;@Configuration&lt;/code&gt; annotated the class that contains the method return the beans or otherwise Spring won’t catch and register it as a bean for your dependency resolution.&lt;br&gt;
Let’s consider this example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;PasswordEncoder&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PasswordEncoder&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;    
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserCreateReq&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;UserEntity&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UserEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPassword&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="na"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt;
                            &lt;span class="err"&gt;…&lt;/span&gt;
                            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;repo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&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;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PasswordEncoderConfiguration&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PasswordEncoder&lt;/span&gt; &lt;span class="nf"&gt;bCryptPasswordEncoder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BCryptPasswordEncoder&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;In this example, I couldn’t change the code base of the bcrypt library so to create the encoder object. I have to use a &lt;code&gt;@Configuration&lt;/code&gt; class to create the bean that I need in a method inside that &lt;code&gt;@Configuration&lt;/code&gt; class. In this way, we could create as many as you want bean objects. And you have to explicitly configure those bean by yourself when the conflict happens. One way to resolve that conflict is by using the &lt;code&gt;@Qualifier&lt;/code&gt; annotation in the constructor of the dependent class.&lt;/p&gt;

&lt;p&gt;That's all. Happy coding.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>annotation</category>
    </item>
  </channel>
</rss>
