<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>

    <title>lealceldeiro.com</title>
    <link>https://lealceldeiro.com</link>
    <atom:link href="https://lealceldeiro.com/rss.xml" rel="self" type="application/rss+xml" />

    <!--https://www.rssboard.org/rss-specification-->
    <description>Latest content in Software Development: technical content, professional experiences, and more.</description>
    <language>en-us</language>
    <pubDate>Mon, 18 May 2026 11:49:26 +0000</pubDate>
    <lastBuildDate>Mon, 18 May 2026 11:49:26 +0000</lastBuildDate>
    <ttl>2880</ttl>
    <generator>JBake</generator>
    <image>
      <url>https://lealceldeiro.com/img/rss_thumbnail.png</url>
      <title>lealceldeiro.com</title>
      <link>https://lealceldeiro.com</link>
      <width>144</width>
      <height>99</height>
    </image>

    <item>

      <title>The strategy pattern in Spring Boot</title>
      <link>https://lealceldeiro.com/articles/2024/the-strategy-pattern-and-springboot.html</link>
      <pubDate>Tue, 9 Jul 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/the-strategy-pattern-and-springboot.html</guid>
      <description>&amp;lt;div id=&amp;quot;toc&amp;quot; class=&amp;quot;toc&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;toctitle&amp;quot;&amp;gt;In this article&amp;lt;/div&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel1&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_strategy_pattern&amp;quot;&amp;gt;The Strategy Pattern&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_whats_spring_boot&amp;quot;&amp;gt;What&amp;amp;#8217;s Spring Boot&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_strategy_pattern_in_spring_boot&amp;quot;&amp;gt;The Strategy pattern in Spring Boot&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_interchangeable_beans&amp;quot;&amp;gt;Interchangeable beans&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel3&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_example_scenario&amp;quot;&amp;gt;The example scenario&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_creating_the_application&amp;quot;&amp;gt;Creating the application&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_endpoint_implementation&amp;quot;&amp;gt;The endpoint implementation&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_first_email_sender&amp;quot;&amp;gt;The first email sender&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_integrating_with_the_first_email_provider&amp;quot;&amp;gt;Integrating with the first email provider&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_the_real_challenge&amp;quot;&amp;gt;The real challenge&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_spring_profiles_to_the_rescue&amp;quot;&amp;gt;Spring profiles to the rescue&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_other_applications&amp;quot;&amp;gt;Other applications&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_strategy_in_spring_boot_itself&amp;quot;&amp;gt;Strategy in Spring Boot itself&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_github_repository&amp;quot;&amp;gt;GitHub repository&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;preamble&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The strategy pattern is one of the well-known software design patterns described in the famous book
&amp;quot;Design Patterns: Elements of Reusable Object-Oriented Software&amp;quot;.
Its usage in Spring Boot offers a clean way to
encapsulate different algorithms and behaviors that makes our applications
more maintainable, flexible, and composed of loosely coupled components.
In this article we&amp;amp;#8217;ll explore what this pattern is about and how to use it effectively in Spring Boot.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_the_strategy_pattern&amp;quot;&amp;gt;The Strategy Pattern&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the words of the renowned authors, the intent of the &amp;quot;Strategy&amp;quot; pattern, also known as &amp;quot;Policy&amp;quot;, is to:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;quoteblock&amp;quot;&amp;gt;
&amp;lt;blockquote&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Define a family of algorithms, encapsulate each one, and make them interchangeable.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Strategy lets the algorithm vary independently from clients that use it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/blockquote&amp;gt;
&amp;lt;div class=&amp;quot;attribution&amp;quot;&amp;gt;
&amp;amp;#8212; Design Patterns: Elements of Reusable Object-Oriented Software
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;What does this mean exactly?&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In simple terms, this means that for a given task, code routine, procedure, any algorithm in general, we can have
several different implementations.
Now, we don&amp;amp;#8217;t need to expose &amp;lt;em&amp;gt;the how&amp;lt;/em&amp;gt; to the clients that consume that algorithm.
Notice, by clients I mean any code that calls the algorithm.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;By following this approach, we ensure our logic is not exposed unnecessarily, and we can evolve it without breaking
any client.
This is one of the fundamentals of
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#what-are-java-classes&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Object-Oriented Programming&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For more details about this pattern, you can check my notes about it in this
&amp;lt;a href=&amp;quot;https://github.com/lealceldeiro/gems/tree/master/DesignPatternsElementsOfReusableObjectOrientesSoftware/Chapter05/Strategy&amp;quot;&amp;gt;GitHub repository&amp;lt;/a&amp;gt;,
or you can read it directly from the mentioned book—this is the &amp;lt;a href=&amp;quot;https://amzn.to/3RIAiAY&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Amazon link&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_whats_spring_boot&amp;quot;&amp;gt;What&amp;amp;#8217;s Spring Boot&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s challenging to find a Java developer nowadays who doesn&amp;amp;#8217;t know what
&amp;lt;a href=&amp;quot;https://spring.io/projects/spring-boot&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Spring Boot&amp;lt;/a&amp;gt; is.
Nevertheless, I once didn&amp;amp;#8217;t know what it was either.
So, let&amp;amp;#8217;s give it a brief intro.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;quoteblock&amp;quot;&amp;gt;
&amp;lt;blockquote&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can &amp;quot;just run&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;We [Spring Boot maintainers] take an opinionated view of the Spring platform and third-party libraries
so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/blockquote&amp;gt;
&amp;lt;div class=&amp;quot;attribution&amp;quot;&amp;gt;
&amp;amp;#8212; Spring Boot documentation
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That quote is almost impossible to simplify further,
but just to make sure new developers understand it,
let&amp;amp;#8217;s put it another way:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Spring Boot builds on top of the Spring framework and its ecosystem as well as Java&amp;amp;#8217;s.
It contains all the capabilities provided by Spring and, additionally, most of the boilerplate code and configuration
needed to run a traditional Spring application.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This way we can use Spring and many other libraries in our projects without having to worry about
all the XML configuration files and code required to only run the application
(to later on start coding the business logic).
Instead, with Spring Boot, we add its dependencies (Maven, Gradle, etc.), some
&amp;lt;a href=&amp;quot;https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-starters/README.adoc&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;starters&amp;lt;/a&amp;gt;,
as needed, and we&amp;amp;#8217;re ready to go by focusing on our business logic.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For a more in-depth look at how Spring and Spring Boot work, I&amp;amp;#8217;d recommend visiting their official websites
or reading &amp;lt;a href=&amp;quot;https://amzn.to/3VHWA74&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Spring Boot 3 and Spring Framework 6&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_the_strategy_pattern_in_spring_boot&amp;quot;&amp;gt;The Strategy pattern in Spring Boot&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s clear that the strategy pattern has many applications, and it&amp;amp;#8217;s not the intention of this article to go through
all of them; instead I&amp;amp;#8217;ll talk about one I find very interesting while using Spring Boot.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;I&amp;amp;#8217;m referring to the capability to declare several components that achieve the same task in different ways,
and use the best fit for a particular task at runtime&amp;amp;#8212;&amp;amp;#8203;I&amp;amp;#8217;ll call it &amp;quot;Interchangeable beans&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To do that, let&amp;amp;#8217;s take an example scenario, so what we build here makes some sense.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The code repository is at the end of the article, so you can check it out locally.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_interchangeable_beans&amp;quot;&amp;gt;Interchangeable beans&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_the_example_scenario&amp;quot;&amp;gt;The example scenario&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The requirements are as follows:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Build a simple web endpoint that will accept a
&amp;lt;a href=&amp;quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;lt;code&amp;gt;POST&amp;lt;/code&amp;gt;&amp;lt;/a&amp;gt; payload and will email a recipient.
The endpoint will receive in the payload the following attributes.
To keep it simple, there will be no validations or security checks performed and only one recipient can be emailed.&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;olist loweralpha&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;loweralpha&amp;quot; type=&amp;quot;a&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt; (string): recipient&amp;amp;#8217;s email address&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;subject&amp;lt;/code&amp;gt; (string): email subject&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;body&amp;lt;/code&amp;gt; (string): email body&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;The application needs to be deployed in different organizations where the internal email provider can be different.
The providers that we&amp;amp;#8217;ll support are: &amp;lt;a href=&amp;quot;https://developers.mailersend.com/&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Mailersend&amp;lt;/a&amp;gt;,
&amp;lt;a href=&amp;quot;https://www.twilio.com/en-us/sendgrid/email-api&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Twilio&amp;lt;/a&amp;gt;,
&amp;lt;a href=&amp;quot;https://www.mailgun.com/products/send/email-api/&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Mailgun&amp;lt;/a&amp;gt;,
and &amp;lt;a href=&amp;quot;https://www.mailjet.com/products/email-api/&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Mailjet&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_creating_the_application&amp;quot;&amp;gt;Creating the application&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;sect4&amp;quot;&amp;gt;
&amp;lt;h5 id=&amp;quot;_project_boilerplate&amp;quot;&amp;gt;Project boilerplate&amp;lt;/h5&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The first thing we need to do is to create the boilerplate for our Spring Boot application.
To do that, we can use many alternatives, being the &amp;lt;a href=&amp;quot;https://start.spring.io/&amp;quot;&amp;gt;Spring initializer&amp;lt;/a&amp;gt; one of the most
commonly used among the community.
Alternatively, if you use &amp;lt;a href=&amp;quot;https://www.jetbrains.com/idea/&amp;quot;&amp;gt;IntelliJ IDEA&amp;lt;/a&amp;gt; Ultimate,
you can also create the project directly from its
&amp;lt;a href=&amp;quot;https://www.jetbrains.com/help/idea/spring-initializr-project-wizard.html&amp;quot;&amp;gt;Spring Boot Project Wizard&amp;lt;/a&amp;gt;.
Regardless of which option you choose, the final result should be the same.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Here&amp;amp;#8217;s what it&amp;amp;#8217;d look like from IntelliJ IDEA:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;imageblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;img src=&amp;quot;images/07/spring-boot-idea-project-wizard.webp&amp;quot; alt=&amp;quot;IntelliJ IDEA Spring Boot project wizard&amp;quot; width=&amp;quot;650&amp;quot; height=&amp;quot;650&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;After the project is created, it&amp;amp;#8217;ll look like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;imageblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;img src=&amp;quot;images/07/spring-boot-app-structure.webp&amp;quot; alt=&amp;quot;Spring Boot app project structure&amp;quot; width=&amp;quot;650&amp;quot; height=&amp;quot;650&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_the_endpoint_implementation&amp;quot;&amp;gt;The endpoint implementation&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s all the boilerplate code needed. Now let&amp;amp;#8217;s focus on our business logic.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The first thing we need to implement is the endpoint that accepts the request with the email information and sends it
to the recipient.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This is going to be as simple as adding the &amp;lt;em&amp;gt;Spring Boot Web&amp;lt;/em&amp;gt; starter.
To do it, I&amp;amp;#8217;ll add the corresponding Maven dependency in the pom.xml file.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-xml hljs&amp;quot; data-lang=&amp;quot;xml&amp;quot;&amp;gt;  &amp;amp;lt;dependencies&amp;amp;gt;
    &amp;amp;lt;!-- ... --&amp;amp;gt;
    &amp;amp;lt;dependency&amp;amp;gt;
      &amp;amp;lt;groupId&amp;amp;gt;org.springframework.boot&amp;amp;lt;/groupId&amp;amp;gt;
      &amp;amp;lt;artifactId&amp;amp;gt;spring-boot-starter-web&amp;amp;lt;/artifactId&amp;amp;gt;
    &amp;amp;lt;/dependency&amp;amp;gt;
    &amp;amp;lt;!-- ... --&amp;amp;gt;
  &amp;amp;lt;/dependencies&amp;amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Now we&amp;amp;#8217;re ready to add the controller class. It&amp;amp;#8217;s as simple as this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@RestController(&amp;quot;email&amp;quot;)
public class EmailController {
  @PostMapping(&amp;quot;/send&amp;quot;)
  public String sendEmail(@RequestBody EmailRequestDto body) {
    // TODO: actually send the email
    return &amp;quot;Email sent&amp;quot;;
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;And when we call the endpoint &amp;lt;code&amp;gt;email/send&amp;lt;/code&amp;gt; it returns &amp;lt;code&amp;gt;Email sent&amp;lt;/code&amp;gt;, as you can see in the image below.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;imageblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;img src=&amp;quot;images/07/spring-boot-controller-endpoint.webp&amp;quot; alt=&amp;quot;Rest Controller&amp;quot; width=&amp;quot;850&amp;quot; height=&amp;quot;750&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;But so far, there&amp;amp;#8217;s no actual email being sent.
It just returns that fancy message.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_the_first_email_sender&amp;quot;&amp;gt;The first email sender&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Up to here this has been straightforward.
Now comes the interesting part.
We need to add a &amp;lt;em&amp;gt;component&amp;lt;/em&amp;gt; that does the
actual job of sending the email message.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s solve the problems one by one. First, let&amp;amp;#8217;s create the component. &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt;, I&amp;amp;#8217;ll call it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class EmailSender {
  public boolean send(String to, String subject, String body) {
      // TODO: implement
      return false;
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Then we can add it to the controller and update the endpoint logic as follows:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@RestController(&amp;quot;email&amp;quot;)
public class EmailController {
  private final EmailSender emailSender;

  public EmailController(EmailSender emailSender) {
    this.emailSender = emailSender;
  }

  @PostMapping(&amp;quot;/send&amp;quot;)
  public String sendEmail(@RequestBody EmailRequestDto request) {
    boolean success = emailSender.send(request.to(), request.subject(), request.body());
    return success ? &amp;quot;Email sent&amp;quot; : &amp;quot;Error sending email&amp;quot;;
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_integrating_with_the_first_email_provider&amp;quot;&amp;gt;Integrating with the first email provider&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Now, let&amp;amp;#8217;s make it work with one provider: let&amp;amp;#8217;s say Mailersend.
To do that, we&amp;amp;#8217;ll follow their
&amp;lt;a href=&amp;quot;https://github.com/mailersend/mailersend-java?tab=readme-ov-file&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;documentation&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;We add the new Maven dependency:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-xml hljs&amp;quot; data-lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;amp;lt;dependency&amp;amp;gt;
  &amp;amp;lt;groupId&amp;amp;gt;com.mailersend&amp;amp;lt;/groupId&amp;amp;gt;
  &amp;amp;lt;artifactId&amp;amp;gt;java-sdk&amp;amp;lt;/artifactId&amp;amp;gt;
  &amp;amp;lt;version&amp;amp;gt;1.0.0&amp;amp;lt;/version&amp;amp;gt;
&amp;amp;lt;/dependency&amp;amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;And we update the sender class as follows:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class EmailSender {
  private static final Logger LOGGER = Logger.getLogger(EmailSender.class.getName());

  @Value(&amp;quot;${integration.mailersend.token}&amp;quot;)
  private String token;

  public boolean send(String to, String subject, String body) {
    Email email = createEmail(to, subject, body);
    MailerSend sender = createSender();

    return sendEmailUsingSender(sender, email);
  }

  private static Email createEmail(String to, String subject, String body) {
    Email email = new Email();
    email.setFrom(&amp;quot;Strategy Pattern In Spring Boot&amp;quot;, &amp;quot;comlealceldeiro@strategy.com&amp;quot;);

    email.addRecipient(to, to);
    email.setSubject(subject);
    email.setPlain(body);
    return email;
  }

  private MailerSend createSender() {
    MailerSend ms = new MailerSend();
    ms.setToken(token);
    return ms;
  }

  private static boolean sendEmailUsingSender(MailerSend sender, Email email) {
    LOGGER.info(&amp;quot;Attempting to send email &amp;quot;);
    try {
      MailerSendResponse response = sender.emails().send(email);
      LOGGER.info(&amp;quot;Sent email with id &amp;quot; + response.messageId);
    } catch (MailerSendException e) {
      LOGGER.warning(&amp;quot;Email not sent&amp;quot;);
      return false;
    }

    return true;
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Notice how we added the field &amp;lt;code&amp;gt;token&amp;lt;/code&amp;gt; to be bound to the Spring Boot property &amp;lt;code&amp;gt;integration.mailersend.token&amp;lt;/code&amp;gt;.
This property can be set in the &amp;lt;code&amp;gt;application.properties&amp;lt;/code&amp;gt; (or yml) file as follows:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-properties hljs&amp;quot; data-lang=&amp;quot;properties&amp;quot;&amp;gt;integration.mailersend.token=eyJhbGciOiJIUzI1NiIsInR0Q8AOZ6RWm_rqbj9tYr8-J4&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock tip&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-tip&amp;quot; title=&amp;quot;Tip&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In an actual production code, it&amp;amp;#8217;s safer to set the token through the cloud provider cli or secrets configuration.
But it&amp;amp;#8217;s never safe to store it in plain text, except (maybe) for local development.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Hurray, our first email is on its way!&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_the_real_challenge&amp;quot;&amp;gt;The real challenge&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;But we face a challenge here:
one of the requirements says the application must be able to run in different organization
and work with different email providers, but if this application is deployed in an environment where the email
provider is different from Mailersend, it&amp;amp;#8217;ll fail.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The most &amp;quot;simple&amp;quot; solution anyone, with the most basic knowledge of Spring Boot, could think of,
is to add a &amp;quot;flag property&amp;quot; in the &amp;lt;code&amp;gt;application.properties&amp;lt;/code&amp;gt; corresponding to each environment.
Then depending on the value of that variable at runtime&amp;amp;mdash;which will be different in each environment&amp;amp;mdash;,
we would use a different logic inside our &amp;lt;code&amp;gt;EmailSender#send&amp;lt;/code&amp;gt; to connect to a different
email API provider.
That&amp;amp;#8217;s it, through some &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;swtich&amp;lt;/code&amp;gt; checks or similar.
Some other alternatives along these lines would work too.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s not an incorrect solution.
However, in the long term, it&amp;amp;#8217;ll become harder to maintain as the class grows bigger over time and new
provider integrations are added.
It&amp;amp;#8217;ll get to the point that it&amp;amp;#8217;ll hinder the development itself.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;A better solution would be creating a different &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt; component for each integration we want to support,
and &amp;quot;load the correct one&amp;quot; at runtime time instead of having a fixed instance in &amp;lt;code&amp;gt;EmailController&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_spring_profiles_to_the_rescue&amp;quot;&amp;gt;Spring profiles to the rescue&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Spring &amp;lt;a href=&amp;quot;https://docs.spring.io/spring-boot/reference/features/profiles.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;profiles&amp;lt;/a&amp;gt; provide a way for us
to segregate parts of an application configuration and make it be available only in certain environment(s).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This feature,
together with configuration components
and spring beans,
is a powerful tool at our disposal here.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock note&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-note&amp;quot; title=&amp;quot;Note&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;A &amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/reference/core/beans/definition.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;spring bean&amp;lt;/a&amp;gt;
is any component we define whose life-cycle is managed by the spring
&amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/reference/core/beans.html#page-title&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;container&amp;lt;/a&amp;gt;.
One of the most common ways to declare a bean is by using the
&amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;lt;code&amp;gt;@Bean&amp;lt;/code&amp;gt;
annotation&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Scoping a component to a specific profile is usually done by using the &amp;lt;code&amp;gt;@Profiles&amp;lt;/code&amp;gt; annotation on the component.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;So, we&amp;amp;#8217;re going to use this feature to have one bean per strategy.
Each strategy will correspond to the logic to send the email through each email provider.
Then, when the application is deployed in a given environment,
the bean (Java class component)
with the strategy that corresponds to the integration for that environment
will be &amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;wired&amp;lt;/a&amp;gt;,
that is it &amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;injected&amp;lt;/a&amp;gt;,
into the component that requires it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect4&amp;quot;&amp;gt;
&amp;lt;h5 id=&amp;quot;_support_to_all_providers&amp;quot;&amp;gt;Support to all providers&amp;lt;/h5&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s code it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;First, we need to find a way to keep a single dependency in our controller.
We don&amp;amp;#8217;t want to declare one dependency per strategy.
To achieve this, we&amp;amp;#8217;ll use an interface: &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt; (we&amp;amp;#8217;ll rename the class with that name, don&amp;amp;#8217;t worry).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;public interface EmailSender {
  boolean send(String to, String subject, String body);
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Next, let&amp;amp;#8217;s update our previous &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt; class.
We&amp;amp;#8217;ll rename it to &amp;lt;code&amp;gt;MailerSendEmailSender&amp;lt;/code&amp;gt; and make it implement &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt;
and override the &amp;lt;code&amp;gt;send&amp;lt;/code&amp;gt; method.
The rest of the code stays the same.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class MailerSendEmailSender implements EmailSender {
  // ... rest of the code unchanged

  @Override
  public boolean send(String to, String subject, String body) {
    // ... rest of the code unchanged
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;By doing this, Spring Boot we&amp;amp;#8217;ll manage the dependency injection for us automatically.
That is, at runtime it will locate a bean that implements &amp;lt;code&amp;gt;EmailSender&amp;lt;/code&amp;gt;
and inject it in the &amp;lt;code&amp;gt;EmailController&amp;lt;/code&amp;gt; class.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;So far this has been a code refactor, we&amp;amp;#8217;re still not giving support to other providers.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s implement the rest of them—I&amp;amp;#8217;ll leave the details out, but the gist should be clear from this example.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Twilio.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class TwilioEmailSender implements EmailSender {

  @Override
  public boolean send(String to, String subject, String body) {
    // ... logic to send emails through the Twilio API
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Mailgun.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class MailgunEmailSender implements EmailSender {

  @Override
  public boolean send(String to, String subject, String body) {
    // ... logic to send emails through the Mailgun API
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Mailjet.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Component
public class MailjetEmailSender implements EmailSender {

  @Override
  public boolean send(String to, String subject, String body) {
    // ... logic to send emails through the Mailjet API
  }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;At this point, if we try to run the application, we&amp;amp;#8217;ll get an error like this one:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-console hljs&amp;quot; data-lang=&amp;quot;console&amp;quot;&amp;gt;Parameter 0 of constructor in com.lealceldeiro.strategysb.EmailController required a single bean, but 4 were found:
	- mailerSendEmailSender: defined in file [~/MailerSendEmailSender.class]
	- mailgunEmailSender: defined in file [~/MailgunEmailSender.class]
	- mailjetEmailSender: defined in file [~/MailjetEmailSender.class]
	- twilioEmailSender: defined in file [~/TwilioEmailSender.class]

This may be due to missing parameter name information

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The framework message is pretty clear: we&amp;amp;#8217;re asking for one bean in &amp;lt;code&amp;gt;EmailController&amp;lt;/code&amp;gt; but Spring found four of them
and there&amp;amp;#8217;s no information that tells it which one to pick over the others.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The options to fix this are also available in the message itself:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Define one of the beans as &amp;lt;em&amp;gt;primary&amp;lt;/em&amp;gt;: annotate one of the implementations as &amp;lt;code&amp;gt;@Primary&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Accept multiple beans in the controller instead of one:
in the controller, receive in the constructor the argument as
&amp;lt;code&amp;gt;List&amp;amp;lt;EmailSender&amp;amp;gt; senders&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;EmailSender emailSender&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Use &amp;lt;code&amp;gt;@Qualifier&amp;lt;/code&amp;gt; in the controller constructor argument: i.e.: &amp;lt;code&amp;gt;@Qualifier(&amp;quot;mailerSendEmailSender&amp;quot;) EmailSender emailSender&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;When we face situations like this one, we must always evaluate what our use case is and what option makes the most sense.
For example, for this particular case using &amp;lt;code&amp;gt;@Primary&amp;lt;/code&amp;gt; for one of the providers would be enough.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;However, remember that our primary goal here was to have one bean/component or the other based on the environment
the application is running on.
This gives us another option to &amp;quot;fix&amp;quot; the previous error and, at the same time,
give a solution to our initial requirement.
We&amp;amp;#8217;ll declare for each component for which profile they&amp;amp;#8217;ll be available.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To do this, we&amp;amp;#8217;ll use the mentioned
&amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;lt;code&amp;gt;@Profile&amp;lt;/code&amp;gt;&amp;lt;/a&amp;gt;
annotation.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Mailersend.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Profile(&amp;quot;mailersend&amp;quot;)
@Component
public class MailerSendEmailSender implements EmailSender {
  // ...
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Twilio.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Profile(&amp;quot;twilio&amp;quot;)
@Component
public class TwilioEmailSender implements EmailSender {
  // ...
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Mailgun.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Profile(&amp;quot;mailgun&amp;quot;)
@Component
public class MailgunEmailSender implements EmailSender {
  // ...
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Mailjet.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;@Profile(&amp;quot;mailjet&amp;quot;)
@Component
public class MailjetEmailSender implements EmailSender {
  // ...
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Have in mind that now, we must specify a profile for our application to start,
otherwise Spring won&amp;amp;#8217;t find the correct bean associated with the profile and will fail.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;From IntelliJ IDEA, you can do it from the
&amp;lt;a href=&amp;quot;https://www.jetbrains.com/help/idea/run-debug-configuration-spring-boot.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Spring Boot Run Configuration&amp;lt;/a&amp;gt;,
like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;imageblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;img src=&amp;quot;images/07/run-debug-configuration-spring-boot.webp&amp;quot; alt=&amp;quot;Spring Boot Run Configuration in IntelliJ IDEA&amp;quot; width=&amp;quot;650&amp;quot; height=&amp;quot;650&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s it for this use case.
From now on, when we start this application by using one of the four available profiles, Spring Boot will locate for us
the component that&amp;amp;#8217;s defined for that given profile and inject it into the &amp;lt;code&amp;gt;EmailController&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For experienced Spring Boot developers, this might look like a trivial example.
But when we implement something like this out of many years of practice,
how many times do we stop and think for a second we&amp;amp;#8217;re using the &amp;quot;Strategy&amp;quot; pattern?
Sometimes it&amp;amp;#8217;s useful to do it ;)&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_other_applications&amp;quot;&amp;gt;Other applications&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;What&amp;amp;#8217;s shown in the previous example is just that: ONE example.
The number of use cases where we can apply it is humongous:
in integration with third party services, sorting algorithms, notification services, file compression,
authentication mechanisms, and much more.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_strategy_in_spring_boot_itself&amp;quot;&amp;gt;Strategy in Spring Boot itself&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Aside from using this pattern in our applications to fulfil specific use cases, it&amp;amp;#8217;s also widely used
in many frameworks, including Spring Boot.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, &amp;lt;a href=&amp;quot;https://spring.io/projects/spring-data&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Spring Data&amp;lt;/a&amp;gt;
provides a flexible way to interact with databases.
Depending on the application&amp;amp;#8217;s requirements, different data access strategies can be used,
such as JDBC, JPA, or any other of the many available.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The same remains true for other techniques related to transaction management, view resolution in Spring MVC,
Spring Security, caching abstractions, message converters, and more.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_github_repository&amp;quot;&amp;gt;GitHub repository&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The example code posted in this article can be found in this
&amp;lt;a href=&amp;quot;https://github.com/lealceldeiro/com-lealceldeiro-strategy-sb&amp;quot;&amp;gt;public GitHub repository&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Understanding access control in Java</title>
      <link>https://lealceldeiro.com/articles/2024/access-control-in-java.html</link>
      <pubDate>Wed, 17 Apr 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/access-control-in-java.html</guid>
      <description>&amp;lt;div id=&amp;quot;toc&amp;quot; class=&amp;quot;toc&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;toctitle&amp;quot;&amp;gt;In this article&amp;lt;/div&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel1&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_whats_access_control_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s &amp;quot;access control&amp;quot; in Java?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_hows_accessibility_to_an_entity_determined&amp;quot;&amp;gt;How&amp;amp;#8217;s accessibility to an entity determined?&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_top_level_classes&amp;quot;&amp;gt;Top level classes&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_members_and_constructors&amp;quot;&amp;gt;Members and constructors&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_array_types&amp;quot;&amp;gt;Array types&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_wrap_up&amp;quot;&amp;gt;Wrap up&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_compilation_units_located_in_different_modules&amp;quot;&amp;gt;Compilation units located in different modules&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#compilation_units_within_the_same_module_or_exported_to_another_modules&amp;quot;&amp;gt;Compilation units within the same module or exported to another module(s)&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;preamble&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The access control mechanisms in Java allow us to have a more secure way of building our applications, libraries,
and code in general.
In this article, you&amp;amp;#8217;ll learn what are the access controls implemented by the Java platform,
how to use them and have a better picture of how they work in general.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_whats_access_control_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s &amp;quot;access control&amp;quot; in Java?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Access control refers to the mechanisms defined by Java
to guarantee that packages, classes, etc. are accessible only to those entities
which have the correct permissions
and not to others which are not entitled to access them.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This sometimes can be seen as a restrictive behavior, but it&amp;amp;#8217;s more than that:
it shields users of a class or package from depending on specific implementation details that are irrelevant to them
and that could potentially break their code if such details are changed.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In short, if access to an entity is permitted, that entity is said to be &amp;lt;em&amp;gt;accessible&amp;lt;/em&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_hows_accessibility_to_an_entity_determined&amp;quot;&amp;gt;How&amp;amp;#8217;s accessibility to an entity determined?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_top_level_classes&amp;quot;&amp;gt;Top level classes&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Top level class can be declared only either &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt; or &amp;lt;em&amp;gt;package private&amp;lt;/em&amp;gt;, any other attempt to specify an access
modifier would result in a compilation error.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_public_classes&amp;quot;&amp;gt;Public classes&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s start with the most permissive one.
If a top level class or interface is declared &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;,
then it may be accessed by any code declared within the same module.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;On top of that, if such a class or interface is a member of a package that is exported by its module,
then it&amp;amp;#8217;ll also be accessible by code located in another module to which the package is exported,
as long as the &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-7.html#jls-7.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;compilation unit&amp;lt;/a&amp;gt;
in which the class or interfaces is declared is visible to that other module
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-6.html#jls-6.6.1&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;§6.6.1&amp;lt;/a&amp;gt;).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In contrast, if the class or interface is part of a module that&amp;amp;#8217;s not exported, it&amp;amp;#8217;ll be accessible only by code
located in the same module.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_package_access&amp;quot;&amp;gt;package access&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;When a top level class or interface is declared as &amp;quot;package private&amp;quot; it&amp;amp;#8217;ll be accessible only from code located within
the same package.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock note&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-note&amp;quot; title=&amp;quot;Note&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;em&amp;gt;package access&amp;lt;/em&amp;gt; is the implicit (default) access given when no modifier (&amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;) is specified.
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_members_and_constructors&amp;quot;&amp;gt;Members and constructors&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;A member (class, interface, field, or method) of a
class, interface, type parameter, or reference type, or a constructor of a class,
is accessible only if (&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-6.html#jls-6.6.1&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;§6.6.1&amp;lt;/a&amp;gt;):&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;the class, interface, type parameter, or reference type is accessible, and&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;the member or constructor is declared to permit access:&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;olist arabic&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;arabic&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;If the member of constructor is declared &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, then access is permitted&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Otherwise, if the member or constructor is declared &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;,
then access is permitted only when:&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;the access occurs from
within the package containing the class in which the protected member or constructor is declared&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;or, the code that tries to access the protected member or constructor of an object
is responsible for the implementation of that object
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-6.html#jls-6.6.2&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;§6.6.2&amp;lt;/a&amp;gt;).&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Otherwise, if  the member or constructor is declared with package access,
then access is permitted only when
the access occurs from
within the package in which the class, interface, type parameter, or reference type is declared.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Otherwise, when the member or constructor is declared &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;,
then access is permitted only when:&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;the access occurs from within the body of the top level class or interface that encloses the declaration of
the member or constructor&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;or, the access occurs in the &amp;lt;code&amp;gt;permits&amp;lt;/code&amp;gt; clause of the top level class or interface that enclose the declaration of
the member&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;or, the access occurs in the
&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.10.1&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;record component list&amp;lt;/a&amp;gt;
of the top level record class that encloses the declaration of the member&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock tip&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-tip&amp;quot; title=&amp;quot;Tip&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
All members of interfaces lacking access modifiers are implicitly &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;.
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_array_types&amp;quot;&amp;gt;Array types&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;An array type is accessible if, and only if, its element type is accessible.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_wrap_up&amp;quot;&amp;gt;Wrap up&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_compilation_units_located_in_different_modules&amp;quot;&amp;gt;Compilation units located in different modules&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;When two compilation units (source files), let&amp;amp;#8217;s say class &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; are located in different modules,
let&amp;amp;#8217;s say &amp;lt;code&amp;gt;moduleA&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;moduleB&amp;lt;/code&amp;gt; respectively,
if class &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is exported by &amp;lt;code&amp;gt;moduleA&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;moduleB&amp;lt;/code&amp;gt;,
then class &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; and its members and constructor(s) might be accessible to class &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;,
following the previously explained rules and summarized in the
&amp;lt;a href=&amp;quot;#compilation_units_within_the_same_module_or_exported_to_another_modules&amp;quot;&amp;gt;next section&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Otherwise, if class &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is not exported from &amp;lt;code&amp;gt;moduleA&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;moduleB&amp;lt;/code&amp;gt;,
it won&amp;amp;#8217;t be accessible by class &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, regardless of the access modifier specified (and the next section doesn&amp;amp;#8217;t apply),
period.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;compilation_units_within_the_same_module_or_exported_to_another_modules&amp;quot;&amp;gt;Compilation units within the same module or exported to another module(s)&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Given we have some compilation units (source files) accessible to one another,
from a module visibility point of view, then we can summarize their accessibility as follows.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the next two tables, you can see, respectively,
the different access levels for top level classes, and for members and constructor.
If you&amp;amp;#8217;re just starting to learn or practice this content, it may be useful to keep them at hand—kind of cheat sheet.
However, make sure you understand all the details and nuances previously explained and, ideally, the content from the
linked resources.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;table class=&amp;quot;tableblock frame-all grid-all fit-content&amp;quot;&amp;gt;
&amp;lt;caption class=&amp;quot;title&amp;quot;&amp;gt;Table 1. Access levels for top level classes, interfaces, etc. &amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt; means accessible, blank means not accessible&amp;lt;/caption&amp;gt;
&amp;lt;colgroup&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;/colgroup&amp;gt;
&amp;lt;thead&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Type (class, interface, etc.)&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Unrelated type within the package containing the accessed type&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Subtype in the same package&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Subtype in a different package&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Any other code that tries to access it&amp;lt;/th&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/thead&amp;gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;em&amp;gt;package private&amp;lt;/em&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock warning&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-warning&amp;quot; title=&amp;quot;Warning&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
Remember, we can&amp;amp;#8217;t declare a top level class, interface, etc. as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;,
otherwise the code won&amp;amp;#8217;t compile.
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;table class=&amp;quot;tableblock frame-all grid-all fit-content&amp;quot;&amp;gt;
&amp;lt;caption class=&amp;quot;title&amp;quot;&amp;gt;Table 2. Access levels for members and constructors. &amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt; means accessible, blank means not accessible&amp;lt;/caption&amp;gt;
&amp;lt;colgroup&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;col&amp;gt;
&amp;lt;/colgroup&amp;gt;
&amp;lt;thead&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Enclosing type (class/interface, etc.)&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Unrelated type within the package containing the enclosing type&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Subtype in the same package&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Subtype in a different package&amp;lt;/th&amp;gt;
&amp;lt;th class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;Any other code that tries to access it&amp;lt;/th&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/thead&amp;gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;em&amp;gt;package private&amp;lt;/em&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;p class=&amp;quot;tableblock&amp;quot;&amp;gt;&amp;lt;code&amp;gt;√&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;tableblock halign-left valign-top&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Quick introduction to Kanban</title>
      <link>https://lealceldeiro.com/articles/2024/kanban.html</link>
      <pubDate>Fri, 12 Apr 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/kanban.html</guid>
      <description>&amp;lt;div id=&amp;quot;toc&amp;quot; class=&amp;quot;toc&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;toctitle&amp;quot;&amp;gt;In this article&amp;lt;/div&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel1&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_getting_started&amp;quot;&amp;gt;Getting started&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_what_is_kanban&amp;quot;&amp;gt;What is Kanban?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_using_kanban&amp;quot;&amp;gt;Using Kanban&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_can_we_apply_it&amp;quot;&amp;gt;Can we apply it?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_how_could_it_help_us&amp;quot;&amp;gt;How could it help us?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_what_changes_are_required_to_use_kanban&amp;quot;&amp;gt;What changes are required to use Kanban?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_how_to_get_started_with_it&amp;quot;&amp;gt;How to get started with it?&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_map_the_workflow&amp;quot;&amp;gt;Map the workflow&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_visualize_the_work_in_progress&amp;quot;&amp;gt;Visualize the work in progress&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_set_initial_wip_limits&amp;quot;&amp;gt;Set initial WIP limits&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_kanban_is_a_pull_system&amp;quot;&amp;gt;Kanban is a &amp;quot;pull&amp;quot; system&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_bottlenecks&amp;quot;&amp;gt;Bottlenecks&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_monitor_the_workflow_and_adapt&amp;quot;&amp;gt;Monitor the workflow and adapt&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_other_considerations&amp;quot;&amp;gt;Other considerations&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_classes_of_services&amp;quot;&amp;gt;Classes of services&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_can_we_use_practices_from_other_agile_methodologies_like_scrum&amp;quot;&amp;gt;Can we use practices from other agile methodologies, like Scrum?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;preamble&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Kanban is a tool that aims to improve efficiency by reducing waste of time and doing things more efficiently.
In this article, we&amp;amp;#8217;ll see a quick introduction to this tool, what it is, how to use it,
and whether it can be applied (and how) to our work process.
The views and knowledge expressed here are those from
 the perspective of a Software Engineer focused on backend development—not an expert in agile methodologies.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_getting_started&amp;quot;&amp;gt;Getting started&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;No matter what business we&amp;amp;#8217;re in, we&amp;amp;#8217;re always finding ways to make things more efficiently.
Lean methodologies were introduced with that purpose,
and they were born in manufacturing environments but can be applied to almost any kind of business activity or process.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;They can be seen as a set of tools that can be adapted or modified to any environment,
according to the problem being solved.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;One of these tools is &amp;lt;strong&amp;gt;Kanban&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_what_is_kanban&amp;quot;&amp;gt;What is Kanban?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In every business, there&amp;amp;#8217;s always a desired result that&amp;amp;#8217;s preceded by a series of steps or tasks.
These steps or tasks almost always carry some form of result, let it be a document, a piece of information,
or any other output that is also used as the input for another step or task.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Not having these results (and on time) can cause delays for the final customer and, consequently, lost in our business.
On the flip side, having too many results (documents, deliverables, etc.) can become cumbersome to manage and can
also have the same undesired effects.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This is where Kanban comes in: it helps us in learning and managing an optimal flow of work within the process.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Some basic rules to implement Kanban are:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;olist arabic&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;arabic&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Visualize the workflow: A visual representation of the workflow lets us see how tasks move from being &amp;quot;not done&amp;quot; to
&amp;quot;done right&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Limit work in process/progress (WIP):
&amp;lt;a href=&amp;quot;https://www.youtube.com/watch?v=hGJpez7rvc0&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Get more done by doing less&amp;lt;/a&amp;gt; (at the same time). There&amp;amp;#8217;s a limit in the
tasks we can do at the same time, going beyond that limit will only decrease our productivity. This is the same
principle we apply in programming: &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;divide and conquer&amp;lt;/a&amp;gt;:
we should decompose &amp;quot;bigger&amp;quot; tasks into smaller ones, and do them one at the time to get them done correctly.
Kanban metrics allow us to find that &amp;quot;optimal&amp;quot; number of tasks.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Measure and improve the flow: Improvements should always be a result of decisions made in an informed way. Kanban
is no different. In order for us to improve those areas where we&amp;amp;#8217;re not being as productive as possible, we need to
first measure the steps, tasks. How much is this task taking and why? How much did we take to release feature XYZ?
How many hours did we spend in the analysis of feature ABC? Finding a good tool that allows us to measure our
tasks/steps properly is very important to enhance our processes. Some examples are
&amp;lt;a href=&amp;quot;https://kanbantool.com/&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Kanbantool&amp;lt;/a&amp;gt; and &amp;lt;a href=&amp;quot;https://www.atlassian.com/software/jira/features/kanban-boards&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Jira&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;One good thing about Kanban is that we don&amp;amp;#8217;t have to change what we may already be doing in our business.
We simply adapt the Kanban tool to our processes, to start measuring, adjusting and optimizing our tasks for
maximum efficiency.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Put in another way: we use Kanban in our existing business to identify ways to improve our already existing processes;
we don&amp;amp;#8217;t need to change our processes at once and &amp;quot;implement Kanban&amp;quot;—we don&amp;amp;#8217;t lose the good things we&amp;amp;#8217;re doing already.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_using_kanban&amp;quot;&amp;gt;Using Kanban&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_can_we_apply_it&amp;quot;&amp;gt;Can we apply it?&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Whenever we consider using some new tool, framework, process, etc. we immediately think: can we apply this?,
will it work for me?&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s no different with Kanban. Can we apply it in the software development processes?&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Yes.
Many would agree with me.
Many others would say &amp;quot;yes, but some Scrum-like methodology would fit better for my use case&amp;quot;—and
that may be completely right as well.
As always, the answer is &amp;quot;it depends&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Software development teams use Kanban to organize their work.
That is, (roughly speaking,) defining a backlog of tasks,
prioritizing them, doing some analysis work,
&amp;quot;moving&amp;quot; them to &amp;quot;development&amp;quot;, &amp;quot;review&amp;quot;, &amp;quot;done&amp;quot;, and the list goes on and on, as long as the teams see it fit.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s the interesting thing about it: it&amp;amp;#8217;s not a restrictive framework that dictates &amp;quot;what must be done&amp;quot;, instead it
offers guides on how things could improve, rather than specific actions, so it can be easily adopted
by different businesses.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_how_could_it_help_us&amp;quot;&amp;gt;How could it help us?&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Kanban should not be &amp;quot;enforced&amp;quot;.
As with any other tool, we must be able to identify pain points we have in our teams
(or solo-team) and check if the tool can really help us.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, if we feel we&amp;amp;#8217;re always busy, and yet we can&amp;amp;#8217;t get anything done/completed.
Or, if we feel the process, as a whole, is not as efficient as it could be.
Or maybe we simply don&amp;amp;#8217;t have an answer to common questions like &amp;quot;where are we at now?&amp;quot;,
&amp;quot;when will task XYZ be completed?&amp;quot;, &amp;quot;who&amp;amp;#8217;s working on feature ABC?&amp;quot;, or &amp;quot;what should I work on now?&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In such cases, it may be worth taking some time and consider something like Kanban; something that could lead
to improve our efficiency and remove waste of time and resources.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_what_changes_are_required_to_use_kanban&amp;quot;&amp;gt;What changes are required to use Kanban?&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Good news!
There&amp;amp;#8217;s no need to overhaul the whole business practices in order for us to adopt Kanban.
It provides the means to visualize what we&amp;amp;#8217;re already doing and which parts could be tweaked to be more efficient.
On top of that, the changes required to make things better
can be implemented gradually—no need to throw everything away and start from scratch!&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_how_to_get_started_with_it&amp;quot;&amp;gt;How to get started with it?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_map_the_workflow&amp;quot;&amp;gt;Map the workflow&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s the most challenging part of almost anything: getting started.
Once we get that little first step, things start to turn smoother.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;So it&amp;amp;#8217;s important to know how to take that first step.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;One approach is to make something like a map or visual representation of our current workflow.
This means identifying major processes in our organization/business, department, area, etc.
It&amp;amp;#8217;s here where we also identify (mentioned in no particular order):&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;which are the steps/tasks we currently have in our process?&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;where are they coming from?&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;how are they defined, assigned and prioritized?&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;With answers to questions like this, we create one column for the Kanban board.
So, for a software development team, we could have the following columns:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Backlog&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Analysis&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Design&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Review&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Development&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Code Review&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Testing&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Release&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Support&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s after we do this step that we can identify improvements in our current workflow, so it&amp;amp;#8217;s normal if some of the
things we&amp;amp;#8217;re doing are not as efficient as we&amp;amp;#8217;d like them to be.
That&amp;amp;#8217;s expected: it&amp;amp;#8217;s from this point from where we start to see areas of improvements and form where we can start to
enhance our flow; so it&amp;amp;#8217;s important to not try to &amp;quot;beatify&amp;quot; the current state of our processes:
we need to see our flaws, so we can tackle them.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Also, at this point, it&amp;amp;#8217;s a good idea to define how many work in progress (WIP) we&amp;amp;#8217;re willing to keep.
For example,
a developer shouldn&amp;amp;#8217;t have more than one feature ticket in state &amp;quot;Development&amp;quot; at the same time;
but probably a marketing specialist can handle three or more tickets with the goal to define different marketing
strategies associated with different partners.
These limits can then be changed gradually as the team monitors their speed and results in each of the steps.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Once we have our workflow steps defined, it&amp;amp;#8217;s time to draw those as columns in a board.
It can be done in almost anything we want: a physical whiteboard, a table, a piece of paper, a corkboard,
a Kanban board on Jira.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_visualize_the_work_in_progress&amp;quot;&amp;gt;Visualize the work in progress&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s time to visualize the work in progress the team is doing.
It&amp;amp;#8217;s now when we start adding the tasks to the board and track how they move from one column (step) to another.
Also, it&amp;amp;#8217;s a good idea to display for each task some meta-data such as
creation date, deadline, priority, type, priority, description, notes, definition of &amp;quot;done&amp;quot;, history and any other
piece of data that helps in monitoring each task.
It&amp;amp;#8217;s good to display on the board as well who&amp;amp;#8217;s working on each task, unless it&amp;amp;#8217;s a solo-team.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Nowadays, tools like Jira, make this part trivial, as its boards already bring all these fields really easy to be edited.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_set_initial_wip_limits&amp;quot;&amp;gt;Set initial WIP limits&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;As mentioned before, it&amp;amp;#8217;s important to define some initials WIP limits.
Sometimes this is challenging to do: how do we know the ideal amount of work in progress we can handle?
But we must start somewhere, right?&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Two approaches can be applied here:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Start with low limits and increase them later as the workflow is monitored: this one can be painful because
we may feel tempted to take more work than the one permitted by the limits,
but it usually gives better results.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Start intentionally with high limits and lower them later as the workflow is monitored: this one may have slower results,
but it could easy the adoption within the team.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;During this time, it&amp;amp;#8217;s good to involve the whole team in documenting the process and setting realistic WIP limits
everyone is willing to enforce.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect3&amp;quot;&amp;gt;
&amp;lt;h4 id=&amp;quot;_why_is_it_so_important_to_respect_the_wip_limits&amp;quot;&amp;gt;Why is it so important to respect the WIP limits?&amp;lt;/h4&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;By working on as many tasks as we can really handle has two major benefits:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;olist arabic&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;arabic&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;It reduces the time we take to get anything done (&amp;lt;em&amp;gt;lead time&amp;lt;/em&amp;gt;)&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;It improves quality by giving greater focus to fewer tasks&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Once these limits have been defined, it&amp;amp;#8217;s good to somehow show them on the board for each of the columns.
This helps in having a visual representation of how many tasks a given team member can be working on simultaneously.
It&amp;amp;#8217;s also important to always keep an eye on everyone&amp;amp;#8217;s&amp;#39; current capacity
and beware of anyone wanting to go beyond the limits.
In such cases, the team could call a meeting to understand what&amp;amp;#8217;s the rationale behind it and discuss it further
if needed.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_kanban_is_a_pull_system&amp;quot;&amp;gt;Kanban is a &amp;quot;pull&amp;quot; system&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The idea of Kanban is that each phase/column &amp;quot;pulls&amp;quot; the work from the previous stage/column, signaling the previous
stage &amp;quot;to make another one&amp;quot; (terms from the manufacturing days).
This is intentional: the idea is that each stage pulls new work only when it&amp;amp;#8217;s possible to complete it.
This way, the WIP limits are enforced for each stage.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This mechanism is opposed to a &amp;quot;push system&amp;quot; where each stage would complete the work on it and would &amp;quot;push&amp;quot; it
to the next stage, regardless of the workload there and without having in mind the WIP limits.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Put in another way: each team member takes (pull) new tasks only when they&amp;amp;#8217;re ready to do the work,
and it becomes his/her responsibility until the task is completed for that stage.
This doesn&amp;amp;#8217;t mean one person can&amp;amp;#8217;t complete the tasks associated to the same concept in different stages,
quite the opposite.
Sometimes it&amp;amp;#8217;s the same person who &amp;quot;moves&amp;quot; a task from one state (column/stage) to another, until it&amp;amp;#8217;s &amp;quot;done&amp;quot;.
Again, Kanban is very flexible, and it&amp;amp;#8217;s here to help us with our flow, not to put restrictions.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_bottlenecks&amp;quot;&amp;gt;Bottlenecks&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Once we have a visual representation of our process(es), ideally in a Kanban board,
we can see at a glance where in the workflow each task is, and it&amp;amp;#8217;s really easy to spot bottlenecks early.
When the WIP limits are set appropriately, we usually hit a limit as soon as a bottleneck starts to form.
That&amp;amp;#8217;s why, in part, the power of Kanban comes from the fact that everyone can see when a problem (like bottleneck)
is forming and take action early in the workflow, before more work is piled up and everyone needs to start jumping
from task to task: this is when clarity is lost because of so many context switches.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_monitor_the_workflow_and_adapt&amp;quot;&amp;gt;Monitor the workflow and adapt&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Kanban doesn&amp;amp;#8217;t force us to monitor, adapt or learn from mistakes; but it&amp;amp;#8217;s generally a good idea to do so.
Otherwise, we&amp;amp;#8217;re not benefitting completely from having a well-defined process structure and WIP limits.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Two tools that are really useful for tracking improvements are &amp;lt;em&amp;gt;lead time&amp;lt;/em&amp;gt; and &amp;lt;em&amp;gt;cycle time&amp;lt;/em&amp;gt;.
Lead time is basically the total time it takes to get something done from the moment someone asks for it until
it&amp;amp;#8217;s completed.
Cycle time is the amount of time spent on a work item by a person or a team up until it&amp;amp;#8217;s ready to be delivered.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s also generally a good idea for the team to review the metrics and reflect on what&amp;amp;#8217;s being done right and what
could be improved.
This is how, iteration after iteration, the team can reach a point of &amp;quot;optimal&amp;quot; flow in the process.
The space and format of this is different for each team.
Some things that I&amp;amp;#8217;ve seen work in the pass is to have regular meetings, let it be weekly, bi-weekly,
or with any other schedule, where everyone is free to express their ideas and observation of how
the workflow has changed since the last iteration.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_other_considerations&amp;quot;&amp;gt;Other considerations&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_classes_of_services&amp;quot;&amp;gt;Classes of services&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Different tasks have different intrinsic value to customers, different risks, different priorities, etc.
By defining &amp;quot;classes of services&amp;quot;, policies, and &amp;quot;rules&amp;quot; associated with them,
we can change the value that&amp;amp;#8217;s delivered and improve the overall workflow.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, in a software development team, there could be a class of service called &amp;quot;production bug&amp;quot;, which would
describe a ticket whose result impacts the customers and the organization directly.
Because of this, resolving it is a top priority
The following &amp;quot;rules&amp;quot; would apply:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;it&amp;amp;#8217;s a top-priority task&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;it can break WIP limits&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;it can skip the steps: Design, Review, and Support&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;it can be released without a product owner approval&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;it has associated a label &amp;quot;deliver ASAP&amp;quot;&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Class of service is a powerful tool that can be implemented and adapted in a variety of ways to optimize the value
delivered by the team.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_can_we_use_practices_from_other_agile_methodologies_like_scrum&amp;quot;&amp;gt;Can we use practices from other agile methodologies, like Scrum?&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Yes, we can.
Kanban doesn&amp;amp;#8217;t define what steps, rituals, meetings, or similar team practices are used in the
team&amp;amp;#8217;s workflow.
It is a tool to be used to improve our workflow, in general, but there&amp;amp;#8217;s no hard rule that stops us from
experimenting and implementing other tools or framework that could also help us.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;After the processes are defined and Kanban is implemented, we must make sure the procedures are explicit.
This way, we make sure everyone understands it as much as possible; and, in case there are doubts, they must
be addressed as soon as possible to make sure the whole team is aligned.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This means, the established policies must be clear, specific, readily available and reviewed/revised regularly.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;We must always keep in mind what&amp;amp;#8217;s the goal of implementing Kanban: to keep improving our processes through
reflection, refinement, experimentation, and creating and managing metrics.
Tools like Jira can help a lot in this sense.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Everything you need to know about the static keyword in Java</title>
      <link>https://lealceldeiro.com/articles/2024/the-static-keyword-in-java.html</link>
      <pubDate>Thu, 22 Feb 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/the-static-keyword-in-java.html</guid>
      <description>&amp;lt;div id=&amp;quot;toc&amp;quot; class=&amp;quot;toc&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;toctitle&amp;quot;&amp;gt;In this article&amp;lt;/div&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel1&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_whats_the_static_keyword_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword in Java?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_use_cases_for_the_static_keyword&amp;quot;&amp;gt;Use cases for the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_static_classes_and_interfaces&amp;quot;&amp;gt;Static classes and interfaces&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_static_fields&amp;quot;&amp;gt;Static fields&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_static_methods&amp;quot;&amp;gt;Static methods&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_static_initializers&amp;quot;&amp;gt;Static initializers&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_static_imports&amp;quot;&amp;gt;Static imports&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_additional_considerations&amp;quot;&amp;gt;Additional considerations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;preamble&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the world of Java programming, the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword plays a significant role,
often thought of as a fundamental concept to grasp.
However, its nuances can sometimes be elusive, especially for those new to the language.
In this article we&amp;amp;#8217;ll go over this construct and some of the details to have in mind when using it in our code.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_whats_the_static_keyword_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword in Java?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;quoteblock&amp;quot;&amp;gt;
&amp;lt;blockquote&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;static&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;[Computing] (of a process or variable) not able to be changed during a set period,
for example, while a program is running.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/blockquote&amp;gt;
&amp;lt;div class=&amp;quot;attribution&amp;quot;&amp;gt;
&amp;amp;#8212; Oxford English Dictionary
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That&amp;amp;#8217;s how the Oxford English Dictionary defines &amp;lt;em&amp;gt;static&amp;lt;/em&amp;gt; as a word in the field of computing.
&amp;lt;em&amp;gt;Not able to change;&amp;lt;/em&amp;gt; that&amp;amp;#8217;s the most important part.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Ok, we didn&amp;amp;#8217;t have to check the dictionary to know that: when something is static, it doesn&amp;amp;#8217;t change.
For example, a &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Lighthouse&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;lighthouse&amp;lt;/a&amp;gt; is static in the sense that it doesn&amp;amp;#8217;t move
from one place to another, like animals do.
Although the lantern(s) and related components move, the tower structure &amp;lt;em&amp;gt;doesn&amp;amp;#8217;t change its position&amp;lt;/em&amp;gt; on the soil.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;How much does this meaning relate to the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword in Java?&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Very much!&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword in Java can be used to modify the definition of
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#what-are-java-classes&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;classes&amp;lt;/a&amp;gt;,
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#fields&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;fields&amp;lt;/a&amp;gt;,
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#methods&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;methods&amp;lt;/a&amp;gt;,
initializers, and more.
It&amp;amp;#8217;s always used to reflect a sense of &amp;lt;em&amp;gt;not changing its [the component&amp;amp;#8217;s] behavior&amp;lt;/em&amp;gt; depending on other factors,
such as an object state.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s see some use cases along with some meaningful examples.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_use_cases_for_the_static_keyword&amp;quot;&amp;gt;Use cases for the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_static_classes_and_interfaces&amp;quot;&amp;gt;Static classes and interfaces&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;As we &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#nested-classes&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;saw previously&amp;lt;/a&amp;gt;,
we can declare inner classes and interfaces, that&amp;amp;#8217;s it, classes and interfaces as members of
other classes and interfaces.
For example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class A {
    class B {
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the previous code, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is an inner class.
It&amp;amp;#8217;s a member of &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; (see &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;tutorial&amp;lt;/a&amp;gt; for a full example).
This is a code smell, because to create an instance of &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, we must create first an instance of &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;var a = new A();
var b = a.new B();&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;or the shorter version:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;var b = new A().new B();&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;That code is almost never correct or justified. Just to mention some of its many flaws, we can say:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;ulist&amp;quot;&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;To create an instance of &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, we must allocate first memory to create an instance of &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;If a declaration of some type (i.e.: a member variable) in a particular scope (i.e.: the inner class)
has the same name as another declaration in the enclosing scope,
then the declaration shadows the declaration of the enclosing scope.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In such cases, the correct approach is to declare a static nested class or interface
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.1.4&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;8.1.1.4&amp;lt;/a&amp;gt;,
&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.1.1.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;9.1.1.3&amp;lt;/a&amp;gt;):&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class A {
    static class B {
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Now &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; no longer &amp;quot;lives within&amp;quot; &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, and we can simply use it like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;var b = new A.B();&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock important&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-important&amp;quot; title=&amp;quot;Important&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Notice we can&amp;amp;#8217;t use &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; as a class or interface modifier
if the class or interface is not a member of another class or interface.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, in the previous code snippet, we can&amp;amp;#8217;t declare the class &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; as &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; or it causes a compilation error.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_static_fields&amp;quot;&amp;gt;Static fields&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In a previous article, we saw how to declare &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#fields&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;fields&amp;lt;/a&amp;gt;.
To make them &amp;lt;em&amp;gt;static&amp;lt;/em&amp;gt; we just need to add the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; modifier.
Let&amp;amp;#8217;s see an example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    static int MAX = 1000;
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the previous snippet, &amp;lt;code&amp;gt;MAX&amp;lt;/code&amp;gt; is a static field.
This means it&amp;amp;#8217;s not related to any specific instance of &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt;.
Instead, it is associated with the class where it&amp;amp;#8217;s declared.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, assuming &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; is located in the same package the following &amp;lt;code&amp;gt;Main&amp;lt;/code&amp;gt; class is located,
then the &amp;lt;code&amp;gt;MAX&amp;lt;/code&amp;gt; field could be used like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;public class Main {
    public static void main(String[] args) {
        System.out.println(Payment.MAX);
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Notice here, how we didn&amp;amp;#8217;t have to &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#constructors&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;instantiate&amp;lt;/a&amp;gt;
&amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; to access the static field.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock tip&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-tip&amp;quot; title=&amp;quot;Tip&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;If we use an object of type &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; to access the static field,
Java ignores the object and accesses the static field through the class,
that&amp;amp;#8217;s it, the instance of the class is only used to know what&amp;amp;#8217;s the type that declares the field.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;A long time ago, I answered a &amp;lt;a href=&amp;quot;https://stackoverflow.com/a/43126188/5640649&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;question on StackOverflow&amp;lt;/a&amp;gt;
where the original poster asked about this, and, despite I learned about it at the time, 6 years later it came back
to bite me in a job interview.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;When I was asked what the following code would print/do, I hesitated to answer correctly and without any doubts:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Order {
    static int basicPackage = 1;
}

public class Main {
    public static void main(String[] args) {
        Order order = new Order();
        System.out.println(order.basicPackage); // prints 1

        order = null;
        System.out.println(order.basicPackage); // prints 1 too
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;I hope you came to the conclusion it prints &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; twice because the object &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt; is only used in this context by Java
to know that this object is of type &amp;lt;code&amp;gt;Order&amp;lt;/code&amp;gt; and get the value of the static field from the type itself,
instead of getting it from the instance.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The declaration of a static field (also known as &amp;lt;em&amp;gt;class variable&amp;lt;/em&amp;gt;) introduces a static context,
which limits the use of constructs that refer to the current object
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.3.1.1&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;8.3.1.1&amp;lt;/a&amp;gt;).
For example, a static field can&amp;amp;#8217;t be accessed from an instance method, or the &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;super&amp;lt;/code&amp;gt; keywords.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This type of construct is useful when declaring constants or variables shared across all instances of a class.
That&amp;amp;#8217;s because the values of such fields are not associated to a specific instance of a class
but rather to the class itself &amp;amp;mdash; hence they&amp;amp;#8217;re also called &amp;lt;em&amp;gt;class variables&amp;lt;/em&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Because of this behavior, any change made to a static field is reflected everywhere it&amp;amp;#8217;s used,
regardless whether it&amp;amp;#8217;s being accessed from one or another instance of the class that declares it,
or even by other classes.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The most common scenario where static fields are used is when combined with the
&amp;lt;a href=&amp;quot;/articles/2024/the-final-keyword-in-java.html#_final_fields&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;final keyword&amp;lt;/a&amp;gt; to create inmutable constants
that we can use throughout our code. For example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;public class Order {
    public static final int BASIC_PACKAGE = 1;
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_static_methods&amp;quot;&amp;gt;Static methods&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Static methods, just like static fields, are regular
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#methods&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;methods&amp;lt;/a&amp;gt; to which the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; modifier
have been applied to.
Let&amp;amp;#8217;s build on top of our previous example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    static int MAX = 1000;

    static boolean isValidTransactionAmount(int amount) {
        return amount &amp;amp;gt; 0 &amp;amp;amp;&amp;amp;amp; amount &amp;amp;lt;= MAX;
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Here we&amp;amp;#8217;ve declared a method &amp;lt;code&amp;gt;isValidTransactionAmount&amp;lt;/code&amp;gt; which is static,
that&amp;amp;#8217;s it, we don&amp;amp;#8217;t need an object of type &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; to use it.
For example, this is how we&amp;amp;#8217;d call it:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;public class Main {
    public static void main(String[] args) {
        System.out.println(Payment.isValidTransactionAmount(20));   // prints true
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Again, static methods involve a static context
from where we don&amp;amp;#8217;t have access to constructs that refer to the current object,
such as instance fields, the &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; keyword, or the &amp;lt;code&amp;gt;super&amp;lt;/code&amp;gt; keyword.
That&amp;amp;#8217;s only expected.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Static fields and methods are constructed when the class is initialized,
not when class instances are constructed, so there&amp;amp;#8217;s no way to access those instance constructs.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_static_initializers&amp;quot;&amp;gt;Static initializers&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Static initializers are almost the same as instance initializers from a syntax perspective.
For example, building on top of the previous example, we could initialize the &amp;lt;code&amp;gt;MAX&amp;lt;/code&amp;gt; variable like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    static int MAX;

    static {
        MAX = 1000;
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This construct is useful when the initialization of a static variable is not simple enough to fit in one line.
In this example, it&amp;amp;#8217;s not worth it, but there are real-world scenarios where there&amp;amp;#8217;s some complex logic we want to
execute when the class is constructed.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;However, this construct should be used carefully because sometimes it&amp;amp;#8217;s not easy to reason about the logic being
implemented.
That&amp;amp;#8217;s because the field declaration and the actual initialization are separated.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The same rules about accessing constructs that refer to the current object apply here.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Additionally, there are a few notes we should remember about static initializers
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.7&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;8.7&amp;lt;/a&amp;gt;):&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;olist arabic&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;arabic&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s a compile-time error if a static initializer can&amp;amp;#8217;t complete normally.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s a compile-time error if a &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; statement appears anywhere within a static initializer.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;For more complex use cases, there are also some exhaustive
&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.3.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;definitions&amp;lt;/a&amp;gt; that you should be aware of.&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_static_imports&amp;quot;&amp;gt;Static imports&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Static imports
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-7.html#jls-7.5.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;7.5.3&amp;lt;/a&amp;gt;)
are used to import static-accessible members from other types, such as classes,
into a given type, for example, another class.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It works by specifying the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword after the &amp;lt;code&amp;gt;import&amp;lt;/code&amp;gt; keyword.
For example, let&amp;amp;#8217;s suppose we have the following &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; class, located in a package named &amp;lt;code&amp;gt;com.payment&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;package com.payment;

public class Payment {
    public static int MAX = 1000;
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;If we have another class called &amp;lt;code&amp;gt;Service&amp;lt;/code&amp;gt; in a package called &amp;lt;code&amp;gt;com.services&amp;lt;/code&amp;gt; and we want to import statically,
the &amp;lt;code&amp;gt;MAX&amp;lt;/code&amp;gt; static field from &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt;, we could do so like this:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;package com.services;

import static com.payment.Payment.MAX;

class Service {
    static {
        System.out.println(MAX);
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It also works for the wildcard (&amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt;) import. This one would import &amp;lt;em&amp;gt;all&amp;lt;/em&amp;gt; static members of &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;import static com.payment.Payment.*;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The alternative to this import is the regular import:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;package com.services;

import com.payment.Payment;

class Service {
    static {
        System.out.println(Payment.MAX);
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_additional_considerations&amp;quot;&amp;gt;Additional considerations&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;While static fields and methods allow us to share common attributed and behaviors defined in a class,
if not used properly they could cause concurrency issues in multithreaded environments.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For that reason we must use them judiciously
and take the appropriate measure to make our data structure thread-safe to avoid data corruption, race conditions,
deadlocks and many of the other well-known concurrency challenges we face in software programming.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Static constructs offer us the flexibility to go beyond simple object-oriented programming (OOP) designs.
With it, we can implement our solutions following a different approach in many cases, including some
functional-programming style.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;From declaring simple constant fields to building &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Pure_function&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;pure functions&amp;lt;/a&amp;gt;,
the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword brings to the Java world the necessary power to be more than just an OOP language.
This may seems superfluous, but given the wide adoption Java has had, it&amp;amp;#8217;s important to keep the language and
platform flexible enough to meet different use cases.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Hopefully, the insights and links shared here are a good reference and a starting point for you to dig deeper
into the intricacies and use cases of the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword and its constructs, to bring their benefits to your
daily coding job; and, as always, be aware of possible pitfalls while using it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Mastering the final keyword in Java</title>
      <link>https://lealceldeiro.com/articles/2024/the-final-keyword-in-java.html</link>
      <pubDate>Thu, 15 Feb 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/the-final-keyword-in-java.html</guid>
      <description>&amp;lt;div id=&amp;quot;toc&amp;quot; class=&amp;quot;toc&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;toctitle&amp;quot;&amp;gt;In this article&amp;lt;/div&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel1&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_whats_the_final_keyword_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword in Java?&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_use_cases_for_the_final_keyword&amp;quot;&amp;gt;Use cases for the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;sectlevel2&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_classes&amp;quot;&amp;gt;Final classes&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_fields&amp;quot;&amp;gt;Final fields&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_methods&amp;quot;&amp;gt;Final methods&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_variables&amp;quot;&amp;gt;Final variables&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;preamble&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword in Java might seem simple at first glance, but it&amp;amp;#8217;s a powerful tool that can significantly
impact our code&amp;amp;#8217;s design, readability, and performance.
In this article, we&amp;amp;#8217;ll delve into the different ways we can use &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; and explore its various benefits for
Java developers.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_whats_the_final_keyword_in_java&amp;quot;&amp;gt;What&amp;amp;#8217;s the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword in Java?&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To put it simple, we say something is &amp;lt;em&amp;gt;final&amp;lt;/em&amp;gt; when it can&amp;amp;#8217;t be &amp;quot;modified&amp;quot;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Imagine a chick that just hatched out of an egg.
That egg is broken, and nothing can revert that; nobody can put together a hatched, or broken, egg, and make the small
chick live inside it again, right?
It&amp;amp;#8217;s the same in Java when something is final.
However, technically speaking, the meaning that something can&amp;amp;#8217;t be &amp;lt;em&amp;gt;modified&amp;lt;/em&amp;gt; takes several forms.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, we can use it when we have a variable holding a reference to an
&amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#constructors&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;object&amp;lt;/a&amp;gt; and we want that variable to keep the same
reference throughout the whole program execution.
This means no other reference can be assigned to it.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;But it can get tricky sometimes. Let&amp;amp;#8217;s see it in details.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_use_cases_for_the_final_keyword&amp;quot;&amp;gt;Use cases for the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword can be applied in several scenarios. Let&amp;amp;#8217;s analyze them, one by one.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_final_classes&amp;quot;&amp;gt;Final classes&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To declare a class &amp;lt;em&amp;gt;final&amp;lt;/em&amp;gt; we use the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; class modifier. For example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;final class Payment {
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;A final class can&amp;amp;#8217;t be extended.
This means no other class can be a subclass of the &amp;lt;em&amp;gt;final&amp;lt;/em&amp;gt; class.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This is useful in situations where we want to limit the inheritance of our type (class),
for example, for security reasons.
It enforces the intended design and prevents unintended modifications through inheritance.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_final_fields&amp;quot;&amp;gt;Final fields&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To declare a field &amp;lt;em&amp;gt;final&amp;lt;/em&amp;gt;, again we use the same keyword.
For example, if we were to add a final field (&amp;quot;amount&amp;quot;) to our previous class, this could be it:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;final class Payment {
    private final BigDecimal amount;

    Payment(BigDecimal amount) {
        this.amount = amount;
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;From now on, this field (&amp;lt;code&amp;gt;amount&amp;lt;/code&amp;gt;) can&amp;amp;#8217;t be assigned a new value, or a compile-time error will be reported
and the code will fail to compile.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;admonitionblock important&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td class=&amp;quot;icon&amp;quot;&amp;gt;
&amp;lt;i class=&amp;quot;fa icon-important&amp;quot; title=&amp;quot;Important&amp;quot;&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;td class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; fields could still be modified through other mechanisms such as
&amp;lt;a href=&amp;quot;https://www.oracle.com/technical-resources/articles/java/javareflection.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;reflection&amp;lt;/a&amp;gt;
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-17.html#jls-17.5.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;17.5.3&amp;lt;/a&amp;gt;),
but that&amp;amp;#8217;s a more complex topic, better covered in its own article.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It&amp;amp;#8217;s also important to have in mind that a &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; field (or variable, for the matter) holds a reference to an object,
and that object can still be mutable regardless.
So, it&amp;amp;#8217;s possible to mutate the state of the object.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For example, suppose we have a field like this: &amp;lt;code&amp;gt;final Map&amp;amp;lt;String, String&amp;amp;gt; map = new HashMap&amp;amp;lt;&amp;amp;gt;()&amp;lt;/code&amp;gt;.
If we call &amp;lt;code&amp;gt;map.add(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&amp;lt;/code&amp;gt; we didn&amp;amp;#8217;t change the reference which &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; points to, but we effectively changed
the value of &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; by adding a new entry.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the previous &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; class example, notice, how we had to declare our own constructor
to accept a value for our &amp;lt;code&amp;gt;amount&amp;lt;/code&amp;gt; field.
This is because all final instance fields must be initialized when the class instance is created.
In the case of the static ones, they must be initialized when the class is loaded.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Other ways to initialize final fields are as follows:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;olist arabic&amp;quot;&amp;gt;
&amp;lt;ol class=&amp;quot;arabic&amp;quot;&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;On the same line where the field is declared. For example:&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;final class Payment {
    private static final BigDecimal MAX_ALLOWED = new BigDecimal(1000_000);
    private final BigDecimal amount = new BigDecimal(100);
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Inside an instance, or static initializer, respectively. For example:&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;final class Payment {
    private static final BigDecimal MAX_ALLOWED;
    private final BigDecimal amount;

    static {
        MAX_ALLOWED = new BigDecimal(1000_000);
    }
    {
        amount = new BigDecimal(100);
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;For the sake of completeness, let&amp;amp;#8217;s mention here the third option:
inside the class constructor (as we saw previously).&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Final fields give us the big benefit of thread safety
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-17.html#jls-17.5&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;17.5&amp;lt;/a&amp;gt;).
This doesn&amp;amp;#8217;t mean that if we declare all fields as &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; in a class that will make the class thread safe;
but it&amp;amp;#8217;s one of the actions we can take towards that goal, and it&amp;amp;#8217;s a simple one.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;It brings also with it more predictable and easy-to-reason-about code and the JVM can also perform low-level
optimization over those fields because of their final nature.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;One common scenario where final fields are used is when combined with the
&amp;lt;a href=&amp;quot;/articles/2024/the-static-keyword-in-java.html#_static_fields&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;static keyword&amp;lt;/a&amp;gt; to create inmutable constants
that we can use throughout our code.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_final_methods&amp;quot;&amp;gt;Final methods&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To declare a final method we use the same &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword. For example:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    private final BigDecimal amount = new BigDecimal(100);

    final BigDecimal getAmount() {
        return amount;
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In this previous example the &amp;lt;code&amp;gt;Payment&amp;lt;/code&amp;gt; class is not &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;.
This means it can be subclassed and its methods overridden or hidden in the children classes
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.4.3.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;8.4.3.3&amp;lt;/a&amp;gt;).
One way to avoid that is to declare those methods as &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;This is a powerful technique: it permits that we have a class that can be extended but certain public behaviors of it
can&amp;amp;#8217;t be changed, not even by its subclasses. This keeps the parent class maintainers in control, and avoid
unexpected bugs.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect2&amp;quot;&amp;gt;
&amp;lt;h3 id=&amp;quot;_final_variables&amp;quot;&amp;gt;Final variables&amp;lt;/h3&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Final variables are also declared by using the same &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword.
For example, in the following method, the argument &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; and the local variable &amp;lt;code&amp;gt;transaction&amp;lt;/code&amp;gt; are final.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    void executeTransaction(final Long id) {
        final Transaction transaction = getCurrentTransaction();
        // rest of the code omitted for brevity
    }
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Something to note here is that if we declare a variable without the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword,
and it&amp;amp;#8217;s never modified again, then it&amp;amp;#8217;s
&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.12.4&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;lt;em&amp;gt;effectively final&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;The use cases for final variables range from code standards and best practices established in some companies,
to language requirements such as lambda expressions.
Let&amp;amp;#8217;s analyze this last one.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.27&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;Lambda expressions&amp;lt;/a&amp;gt; require that any local
variable, formal parameter, or exception parameter used but not declared in a lambda expression
must either be final or effectively final
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.27.2&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;15.27.2&amp;lt;/a&amp;gt;).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Let&amp;amp;#8217;s take as an example the following code:&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;class Payment {
    void executeTransaction(Long id) {
        Transaction transaction = getTransaction();
        queueTasks(() -&amp;amp;gt; System.out.printf(&amp;quot;Queued tasks for transaction %s and id %s&amp;quot;, transaction, id));
    }

    Transaction getTransaction() { return new Transaction(); }

    void queueTasks(Runnable callback) {
        // code omitted for brevity
        callback.run();
    }
}
class Transaction {}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;For the sake of simplicity, let&amp;amp;#8217;s focus only on the method &amp;lt;code&amp;gt;executeTransaction&amp;lt;/code&amp;gt; (and ignore the auxiliary methods).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;listingblock&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;content&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;highlightjs highlight&amp;quot;&amp;gt;&amp;lt;code class=&amp;quot;language-java hljs&amp;quot; data-lang=&amp;quot;java&amp;quot;&amp;gt;void executeTransaction(Long id) {
    Transaction transaction = getTransaction();
    queueTasks(() -&amp;amp;gt; System.out.printf(&amp;quot;Queued tasks for transaction %s and id %s&amp;quot;, transaction, id));
}&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In its body, there&amp;amp;#8217;s a call to &amp;lt;code&amp;gt;queueTasks&amp;lt;/code&amp;gt; which accepts a &amp;lt;code&amp;gt;Runnable&amp;lt;/code&amp;gt; parameter.
Because
&amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Runnable.html&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;lt;code&amp;gt;Runnable&amp;lt;/code&amp;gt;&amp;lt;/a&amp;gt;
is a &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;functional interface&amp;lt;/a&amp;gt;
we can provide the argument as a lambda expression.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;In the body of the lambda expression the variable &amp;lt;code&amp;gt;transaction&amp;lt;/code&amp;gt; and the parameter &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; are used
without being declared &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; explicitly.
This is possible because they are &amp;lt;em&amp;gt;effectively final&amp;lt;/em&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;If we were to re-assign a new value to either &amp;lt;code&amp;gt;transaction&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; then they would stop being effectively final.
This would cause a compile-time error reported when either of them is first accessed inside the lambda body.
The message would say something like &amp;lt;code&amp;gt;Variable used in lambda expression should be final or effectively final&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;To avoid such error, we can declare them as &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; and, if we ever re-assign a new value to them, we get a
compile-time error on the line where the re-assignment happens.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Declaring &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; variables is also useful in scenarios where there are many variables declared
(common in long methods from legacy code), and we want to make sure none of them is re-assigned a new value.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;sect1&amp;quot;&amp;gt;
&amp;lt;h2 id=&amp;quot;_final_thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;
&amp;lt;div class=&amp;quot;sectionbody&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Now that we&amp;amp;#8217;ve explored what&amp;amp;#8217;s the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; keyword used for and how to apply it, it&amp;amp;#8217;s time for you to put that
knowledge to work on your daily job.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Look again that class design and ask yourself: Is this class supposed to be subclassed?
If yes, then document it, if not, then make it final. Your future self will thank you. You can always come
back and make the class non-final, but you can&amp;amp;#8217;t make a class final without possibly breaking other code that could
have extended your class
(&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-13.html#jls-13.4.2.3&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;noopener&amp;quot;&amp;gt;&amp;amp;sect;13.4.2.3&amp;lt;/a&amp;gt;).&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;Questions like this should pop out of your mind when you design and implement your solutions. It will help with the
overall code quality and the quality of the final product, which means happier final users.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;One final word of &amp;lt;strong&amp;gt;caution&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;paragraph&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;With great power comes great responsibility.
While &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; is powerful, its overuse can lead to inflexible code.
We should use it judiciously, considering the trade-offs between immutability and flexibility
and striving to achieve the correct balance.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Sealed classes in Java</title>
      <link>https://lealceldeiro.com/articles/2024/sealed-classes-in-java.html</link>
      <pubDate>Mon, 5 Feb 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/sealed-classes-in-java.html</guid>
      <description>&amp;lt;h2 id=&amp;quot;what-are-sealed-classes-in-java&amp;quot;&amp;gt;What are sealed classes in Java?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    In a &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;previous article&amp;lt;/a&amp;gt; we learned
    what&amp;#39;s a Java class. From a code perspective, sealed classes are not far from a regular class, but they&amp;#39;re somehow
    special in the way they can be extended. Let&amp;#39;s see it in details.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;sealed-keyword&amp;quot;&amp;gt;The &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; keyword&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    In the aforementioned post there&amp;#39;s a reference to a
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        class modifier
    &amp;lt;/a&amp;gt; called &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;. This is the differentiator, from a code perspective, of a sealed class from a
    regular class.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    A class is sealed when this keyword is used in its declaration. More formally, it&amp;#39;s a class of which all direct
    subclasses are known when it&amp;#39;s declared, and no other direct subclasses are desired or required (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.1.2&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;8.1.1.2
    &amp;lt;/a&amp;gt;). So, its declaration is the same we
    &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#structure&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;saw already&amp;lt;/a&amp;gt;, with the
    specificity that &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; must be present as a class modifier:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{ClassModifier} class TypeIdentifier [TypeParameters] [ClassExtends] [ClassImplements] [ClassPermits] ClassBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s see an example:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed class PrimaryColor {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h2 id=&amp;quot;uses-of-sealed-classes&amp;quot;&amp;gt;Use cases for sealed classes&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Sealed classes, being a &amp;lt;a href=&amp;quot;https://openjdk.org/jeps/409&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;relatively new addition&amp;lt;/a&amp;gt; to Java,
    have enabled developers to use some features and paradigms more easily. For example, they, together with
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/language/records.html#GUID-6699E26F-4A9B-4393-A08B-1E47D4B2D263&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;
        records
    &amp;lt;/a&amp;gt;, and
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html#GUID-E69EEA63-E204-41B4-AA7F-D58B26A3B232&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;
        pattern matching
    &amp;lt;/a&amp;gt;, have made much easier to write our code following a
    &amp;lt;a href=&amp;quot;https://www.infoq.com/articles/data-oriented-programming-java/&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        data-oriented programming
    &amp;lt;/a&amp;gt; approach.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    I would say this is one of the cases where they fit the best: they allow us to represent in our code the real-world
    data structures more accurately in scenarios where inheritance is needed, but limited to only just some known
    subtypes, and where, therefore, &amp;quot;traditional&amp;quot; inheritance is not a perfect fit.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This means with sealed classes we can create &amp;lt;em&amp;gt;closed type hierarchies&amp;lt;/em&amp;gt;,
    &amp;lt;em&amp;gt;limited set of implementations&amp;lt;/em&amp;gt;, &amp;lt;em&amp;gt;state machines&amp;lt;/em&amp;gt;, &amp;lt;em&amp;gt;limited set of exceptions&amp;lt;/em&amp;gt;, and more,
    everything revolving around the main capability of limiting the number of subtypes that can be defined for a given
    class or interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This explicit and exhaustive control over the direct subtypes is useful when the class hierarchy is used to model
    the types of values in a domain, rather than as a mechanism for code inheritance and reuse.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;sealed-classes-example&amp;quot;&amp;gt;Example&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s consider the previous class, &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt;; it represents a primary color (concept from our
    real-world business domain). For the sake of simplicity, let&amp;#39;s assume red, yellow, and blue are the only possible
    primary colors in our business domain
    &amp;lt;a href=&amp;quot;#primaryColorsFootNote&amp;quot; target=&amp;quot;_blank&amp;quot; class=&amp;quot; link-underline link-underline-opacity-0&amp;quot;&amp;gt;&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;&amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This naturally leads us to want to have a superclass &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; and only three subclases:
    &amp;lt;code&amp;gt;Red&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Yellow&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;Blue&amp;lt;/code&amp;gt;. With this setup we have the data structure that
    represents our data, modeled as Java classes/objects. Before sealed classes came along, the code would have looked
    like this:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class PrimaryColor {
}

class Red extends PrimaryColor {
}

class Yellow extends PrimaryColor {
}

class Blue extends PrimaryColor {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Then other developers could use these classes to interact with our
    &amp;lt;a href=&amp;quot;/articles/2024/java-interfaces.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;API&amp;lt;/a&amp;gt;. Maybe they need to send a piece of data,
    packed as one of these three classes. Something like this:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
PrimaryColor color = new Red();
api.send(color);    // details of the `api` object are omitted here for brevity
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    That&amp;#39;s what we were used to. It represents perfectly our business domain &amp;amp;mdash; well, almost. There&amp;#39;s
    one design flaw: we&amp;#39;re assuming only &amp;lt;em&amp;gt;red&amp;lt;/em&amp;gt;, &amp;lt;em&amp;gt;yellow&amp;lt;/em&amp;gt;, and &amp;lt;em&amp;gt;blue&amp;lt;/em&amp;gt; could be sent as possible
    values through our &amp;lt;code&amp;gt;api&amp;lt;/code&amp;gt; object, but that&amp;#39;s not entirely true.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    If other developers decide to extend our &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; class, or any of its subtypes, with a class of
    their own, they could send any other color, for example &amp;lt;code&amp;gt;Pink&amp;lt;/code&amp;gt; which is not &amp;lt;em&amp;gt;primary&amp;lt;/em&amp;gt;. They could
    even send an object &amp;lt;code&amp;gt;apple&amp;lt;/code&amp;gt;, which is not a color, but a fruit!
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    So, how do we guarantee that only the specific classes we supply as possible values for &amp;lt;em&amp;gt;primary colors&amp;lt;/em&amp;gt;
    are used? The answer lies in &amp;lt;em&amp;gt;sealed classes&amp;lt;/em&amp;gt;. To be fair, there could be other options such as using the
    &amp;lt;code&amp;gt;instanceof&amp;lt;/code&amp;gt; operator, but they&amp;#39;re not as clean as using a sealed class, that&amp;#39;s why I won&amp;#39;t even show
    the code with that alternative.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Back to sealed classes, let&amp;#39;s remodel, our code to use this feature:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed class PrimaryColor {
}

final class Red extends PrimaryColor {
}

final class Yellow extends PrimaryColor {
}

final class Blue extends PrimaryColor {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Notice how &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; was declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; and its subclasses were declared
    &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;. This is an improvement. &amp;lt;code&amp;gt;Red&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Yellow&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;Blue&amp;lt;/code&amp;gt; cannot be
    further extended; and we&amp;#39;re showing intent: anyone reading the code will realize we&amp;#39;re trying to limit the possible
    subclasses our &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; can have.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Sadly, good intentions are not enough. Despite declaring a class as &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; and the subclasses
    &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;, other classes out of our control could still extend the superclass and be provided as a valid
    &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; value. For example, this is valid:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
// somewhere, in an external code
final class Pink extends PrimaryColor {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    To fix this, we need to use another keyword in combination with &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;permits-keyword&amp;quot;&amp;gt;The &amp;lt;code&amp;gt;permits&amp;lt;/code&amp;gt; keyword&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    The &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause specifies all the classes intended as direct subclasses of the class being declared
    (&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.6&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;8.1.6&amp;lt;/a&amp;gt;)
    and
    its structure is as follows:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
permits TypeName {, TypeName}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    This means that any other class that is declared to extend our superclass and which is not present in the
    superclass&amp;#39; &amp;lt;em&amp;gt;permit&amp;lt;/em&amp;gt; clause will receive a compile-time error.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s revise our previous code to specify the permitted classes:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed class PrimaryColor permits Blue, Red, Yellow {
}

final class Red extends PrimaryColor {
}

final class Yellow extends PrimaryColor {
}

final class Blue extends PrimaryColor {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    By doing this there&amp;#39;s no way anyone can extend &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; further (without us allowing it first in
    the superclass declaration itself).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    After this change, the class declaration for &amp;lt;code&amp;gt;Pink&amp;lt;/code&amp;gt; would fail and no object of that
    type can be sent where a &amp;lt;code&amp;gt;PrimaryColor&amp;lt;/code&amp;gt; is expected.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;sealed-classes-considerations&amp;quot;&amp;gt;Considerations&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    There are some important aspects to keep in mind when working with sealed classes:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        Interfaces can be declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; as well &amp;amp;mdash; as you already might have inferred from previous
        mentions to this.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        A class/interface cannot be declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; without the &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause and without another
        class/interface implementing/extending it. So, for example, this code alone will report a compile-time error:
        &amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed interface Processor {
}
        &amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
        To make it compile we could have a class to implement it &amp;amp;mdash; something like this:
        &amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
final class OrderProcessor implements Processor {
}
        &amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        A class is said to be &amp;lt;em&amp;gt;freely extensible&amp;lt;/em&amp;gt; if its direct superclass is not &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;,
        and none of its direct superinterfaces are &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, and it&amp;#39;s neither &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;
        nor &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; itself. Otherwise, such a class is freely extensive if and only if it is declared
        &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt; (and is not &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;, of course).
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Related to the previous point, if a class has a &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; direct superclass or a &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;
        direct superinterface, it&amp;#39;s a compilation error if such class is not declared &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;,
        &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;, either explicitly or implicitly; that&amp;#39;s because when a
        class is declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, it forces all direct subclasses to explicitly declare whether
        they are &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Enums are either &amp;lt;em&amp;gt;implicitly&amp;lt;/em&amp;gt; &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; or &amp;lt;em&amp;gt;implicitly&amp;lt;/em&amp;gt; &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, so they can
        implement a sealed interface. Similarly, a record class is &amp;lt;em&amp;gt;implicitly&amp;lt;/em&amp;gt; &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;, so it can
        also implement a sealed interface.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        A class can not be declared &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;, unless it has either a sealed direct superclass or a
        sealed direct superinterface &amp;amp;mdash; otherwise a compilation error is reported.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        The &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause cannot be used without the &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; modifier in the type declaration.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Every permitted direct subclass specified by the &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause must be a direct subclass of the
        superclass or a compile-time error occurs.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Every type listed in the &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause must be accessible at compile-time and must be specified only
        once, otherwise the code won&amp;#39;t compile.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        If a sealed class &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; is associated with a named module (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-7.html#jls-7.3&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;7.3
        &amp;lt;/a&amp;gt;) , then every class specified in the permits clause of &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt;&amp;#39;s declaration must be associated with
        the same module as &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt;, or a compile-time error is reported
        (&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.6&amp;quot;
            target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;8.1.6&amp;lt;/a&amp;gt;).
        This makes perfect sense, as supertypes and subtypes must be &amp;lt;em&amp;gt;resolvable&amp;lt;/em&amp;gt;/accessible during compile-time
        and for this to be 100% guaranteed, they must be located in the same module (in modular systems). Also, any
        class hierarchy structure should be declared in the same business domain unit (module), where maintenance is
        provided by the same development team.
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h2&amp;gt;Sealed classes without permitted subtypes&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Strictly speaking, a class or interface can be declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; without declaring the permitted
    subtypes, as long as the above considerations are taken into account. When this happens, the permitted subtypes
    are automatically inferred to those which are direct subtypes.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Care must be taken if this approach is used: sealed classes are usually intended to restrict a type hierarchy. If we
    don&amp;#39;t explicitly list the possible subtypes by using the &amp;lt;em&amp;gt;permits&amp;lt;/em&amp;gt; clause, then in future other subtypes can
    be added with unintended consequences, such as getting a compile-time error on a &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; case because
    it&amp;#39;s non-exhaustive anymore.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;sealed-classes-final-thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Sealed classes are yet another great improvement to the Java language and platform, that gives developers more
    options to build secure robust and predictable software. It specifically allows us to model different kinds
    of a business domain in our code with precision and without resorting to weird workarounds or twisted class
    hierarchies.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Together with other language constructs, sealed classes, enhance our code and make it more succinct, readable,
    and maintainable, which leads towards more productive teams. If this articles doesn&amp;#39;t cover everything about sealed
    classes, it is a good start for you to read and investigate more about this topic so you can put it in practice
    in your day-to-day job.
&amp;lt;/p&amp;gt;

&amp;lt;hr/&amp;gt;

&amp;lt;p id=&amp;quot;primaryColorsFootNote&amp;quot;&amp;gt;
    &amp;lt;sup&amp;gt;&amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Primary_color&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;1&amp;lt;/a&amp;gt;&amp;lt;/sup&amp;gt; The
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Primary_color&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt; colors system&amp;lt;/a&amp;gt; is complex. This post
    doesn&amp;#39;t attempt to explain it in details. It doesn&amp;#39;t either affirm that read, yellow, and blue are
    &amp;lt;strong&amp;gt;the&amp;lt;/strong&amp;gt; primary colors.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Abstract classes in Java</title>
      <link>https://lealceldeiro.com/articles/2024/abstrat-classes-in-java.html</link>
      <pubDate>Sun, 28 Jan 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/abstrat-classes-in-java.html</guid>
      <description>&amp;lt;h2 id=&amp;quot;what-are-abstract-classes-in-java&amp;quot;&amp;gt;What are abstract classes in Java?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    In a &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;previous post&amp;lt;/a&amp;gt; we learned
    what&amp;#39;s a Java class. To be more precise, the examples mentioned there are &amp;lt;em&amp;gt;concrete&amp;lt;/em&amp;gt; classes. Usually, when
    there&amp;#39;s no mention whether a class is concrete or abstract, it&amp;#39;s inferred we&amp;#39;re talking about a concrete class.
    Likewise, if we want to talk about an abstract one, we usually make the distinction by saying &amp;lt;em&amp;gt;... an abstract
    class...&amp;lt;/em&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    So, then, when is a class abstract?
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;abstract-class-keyword&amp;quot;&amp;gt;The &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; keyword&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    A class is abstract when the &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; keyword (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        ClassModifier
    &amp;lt;/a&amp;gt;) is used in the class declaration. It is a class that is incomplete, or to be considered incomplete (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.1.1&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;8.1.1.1
    &amp;lt;/a&amp;gt;). Hence, the syntax for the full declaration of an abstract class is the same
    we &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#structure&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;saw already&amp;lt;/a&amp;gt;:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{ClassModifier} class TypeIdentifier [TypeParameters] [ClassExtends] [ClassImplements] [ClassPermits] ClassBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s see an example&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
abstract class Animal {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h2 id=&amp;quot;abstract-classes-abstract-methods&amp;quot;&amp;gt;Abstract methods&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Ok, the code looks simple. By just adding &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; to our class declaration we made it abstract. But
    what&amp;#39;s so especial about it?
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Well, when a class is abstract we can declare abstract methods in its
    &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#class-body&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;body&amp;lt;/a&amp;gt;. An abstract method
    is a method without a body (no implementation). Then, subclasses that extend the abstract class must provide
    concrete implementations for these abstract methods. For example:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
abstract class Animal {
    abstract void move();
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h2 id=&amp;quot;uses-of-abstract-classes&amp;quot;&amp;gt;Use cases for abstract classes&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Before seeing some elaborated examples, let&amp;#39;s understand this: why would we want to declare an abstract class to
    begin with?
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Let&amp;#39;s recap: we use classes to model our domain, our real world entities and concepts as well as their interactions
    with each other. And, as you might guess, some of those concept and entities are represented in our minds as
    abstract ones.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    For example: if we&amp;#39;re modeling a software for a Zoo, we need to model animals, the staff, food,
    cages, etc. Some of these concepts need to be modeled at a higher level of abstraction to represent them properly.
    Imagine we want to have a class &amp;lt;code&amp;gt;Tiger&amp;lt;/code&amp;gt; to handle information about tigers, &amp;lt;code&amp;gt;Snake&amp;lt;/code&amp;gt; to handle
    information about the snakes in the Zoo, &amp;lt;code&amp;gt;Crocodile&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Parrot&amp;lt;/code&amp;gt;, ... you get the idea.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    All of these animals have many things in common; for example, they all move, they eat, they breathe, and so on. But
    they also have many differences: some of them walk, some crawl, some fly, etc.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    It&amp;#39;s exactly because of their differences that we need to create separate classes for each of them &amp;amp;mdash;that&amp;#39;s
    clear&amp;amp;mdash;. But this leaves us with a problem, what do we do with the things (
    &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#fields&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;properties&amp;lt;/a&amp;gt;
    and &amp;lt;a href=&amp;quot;/articles/2024/java-oop-classes-and-objects.html#methods&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;behaviors&amp;lt;/a&amp;gt;) they share?
    Do we just duplicate those fields and methods in each class? That doesn&amp;#39;t sound good. Let&amp;#39;s see an example of how
    it would look like:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Tiger {
    String name;
    int energy;

    Tiger(String name) {
        this.name = name;
        energy = 100;
    }

    void move() {
        energy -= 3;
        System.out.println(name + &amp;quot; walked&amp;quot;);
    }

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}

class Snake {
    String name;
    int energy;

    Snake(String name) {
        this.name = name;
        energy = 100;
    }

    void move() {
        energy -= 2;
        System.out.println(name + &amp;quot; crawled&amp;quot;);
    }

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}

class Parrot {
    String name;
    int energy;

    Parrot(String name) {
        this.name = name;
        energy = 100;
    }

    void move() {
        energy--;
        System.out.println(name + &amp;quot; flew&amp;quot;);
    }

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    There&amp;#39;s a LOT of duplication in that code! Actually, everything, except for the &amp;lt;code&amp;gt;move&amp;lt;/code&amp;gt; method is the
    same. This design is difficult to maintain at a bigger scale. Imagine a production code base with hundreds of
    classes and several team members working simultaneously on it. That&amp;#39;s not how we should design it for the
    long term.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This is one of the scenarios where abstract classes shine. They allow us to represent abstract concepts and
    &amp;quot;delegate&amp;quot; the specificities to the concrete classes that implement them.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;examples&amp;quot;&amp;gt;Example&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s refactor the previous code to use an abstract class to keep the same fields and methods in it and delegate the
    specifics of each animal to the subclasses.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
abstract class Animal {
    String name;
    int energy;

    Animal(String name) {
        this.name = name;
        energy = 100;
    }

    abstract void move();

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}

class Tiger extends Animal {
    Tiger(String name) {
        super(name);
    }

    @Override
    void move() {
        energy -= 3;
        System.out.println(name + &amp;quot; walked&amp;quot;);
    }
}

class Snake extends Animal {
    Snake(String name) {
        super(name);
    }

    @Override
    void move() {
        energy -= 2;
        System.out.println(name + &amp;quot; crawled&amp;quot;);
    }
}

class Parrot extends Animal {
    Parrot(String name) {
        super(name);
    }

    @Override
    void move() {
        energy--;
        System.out.println(name + &amp;quot; flew&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Now, this is better. Now we can see clearly what fields and methods belong to an &amp;lt;em&amp;gt;&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;&amp;lt;/em&amp;gt;.
    Everything else belongs to the details of specific classes (&amp;lt;code&amp;gt;Tiger&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Parrot&amp;lt;/code&amp;gt;, etc.)
&amp;lt;/p&amp;gt;

&amp;lt;blockquote&amp;gt;
    Notice in the previous code snippet we&amp;#39;ve used the
    &amp;lt;a href=&amp;quot;/articles/2024/java-annotation-interfaces.html#interface-examples&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; annotation
    &amp;lt;/a&amp;gt; to make sure we&amp;#39;re overriding properly the &amp;lt;code&amp;gt;move&amp;lt;/code&amp;gt; method.
&amp;lt;/blockquote&amp;gt;

&amp;lt;h2 id=&amp;quot;instantiation&amp;quot;&amp;gt;Instantiation&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Now we can use the abstract class and its subclasses in our code, but, because abstract classes cannot be
    instantiated directly by using the &amp;lt;code&amp;gt;new&amp;lt;/code&amp;gt; keyword (see next section), we need to create objects of the
    concrete subclasses and use them polymorphically.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public static void main(String[] args) {
    Animal teo = new Tiger(&amp;quot;Teo&amp;quot;);
    Animal sisi = new Snake(&amp;quot;Sisi&amp;quot;);
    Animal pepi = new Parrot(&amp;quot;Pepi&amp;quot;);

    teo.move();
    teo.eat();

    sisi.move();
    sisi.eat();

    pepi.move();
    pepi.eat();
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;This will print in console:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-shell&amp;quot;&amp;gt;
Teo walked
Teo ate
Sisi crawled
Sisi ate
Pepi flew
Pepi ate
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    No matter all the objects (&amp;lt;code&amp;gt;teo&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sisi&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;pepi&amp;lt;/code&amp;gt;) are stored in a variable
    (reference) of type &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;, their specific types are different at runtime (such as &amp;lt;code&amp;gt;Tiger&amp;lt;/code&amp;gt;)
    and so their behaviors are different (different texts printed in console).
&amp;lt;/p&amp;gt;

&amp;lt;h2&amp;gt;Considerations&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    With abstract classes (and &amp;lt;a href=&amp;quot;/articles/2024/java-interfaces.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;interfaces&amp;lt;/a&amp;gt;) we can
    simplify complex concepts in the software by modeling these classes based on the real-world concepts we&amp;#39;re
    representing. We could talk infinitely about many of the advantages of this design. Some of the most remarkable ones
    are:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;It ensures consistent behavior and at the same time it permits various implementations&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;Data can now be encapsulated more easily in each class, separately&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;Clear separation of boundaries, concerns and responsibilities of each class&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;More cohesive data structure, without unnecessary code duplication&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;Classes focused on &amp;quot;doing one thing well&amp;quot; &amp;amp;mdash; without bloating their code&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;Code base easier to maintain, adapt and scale&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;
    However, they&amp;#39;re not a silver bullet, we must use them responsibly and have in mind what&amp;#39;s their purpose and the
    problem they solve:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        Abstract classes can also have regular (concrete) methods with a complete implementation. These methods can be
        inherited by the subclasses without any need for overriding.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        It is a compile-time error if an attempt is made to create an instance of an abstract class using a class
        instance creation expression (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.9.1&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;15.9.1
        &amp;lt;/a&amp;gt;). For example, the following code would report a compilation error:
        &amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
            Animal animal = new Animal(&amp;quot;Teo&amp;quot;); &amp;amp;lt; -- compilation error!
        &amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
        The closest we can get the previous code is to use an anonymous class &amp;amp;mdash; but this is something completely
        different from what we&amp;#39;re discussing here. Let&amp;#39;s see it, for the sake of completeness:
        &amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
                    Animal animal = new Animal(&amp;quot;Tiger&amp;quot;) {
                        @Override
                        void move() {
                            energy -=5;
                            System.out.println(name + &amp;quot; moved in an &amp;#39;anonymous&amp;#39; way&amp;quot;);
                        }
                    };
        &amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        We can not declare &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; methods in a non-&amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; class (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.1.1&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;8.1.1.1
        &amp;lt;/a&amp;gt;).
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Every constructor of a class extending an abstract class must call (directly, or indirectly) the constructor
        of the abstract class. In our previous example this corresponds to the line &amp;lt;code&amp;gt;super(name);&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; methods are inherited. This means if a class inherits some abstract method, that class
        must either: implement the method (override it), or the class itself must be declared &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt;
        as well. This case would be useful if we want to represent another level of abstraction. For example, if we&amp;#39;d
        want to model in our software some different types of animal, such as
        &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Mammal&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;lt;em&amp;gt;mammals&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;,
        &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Reptile&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;lt;em&amp;gt;reptiles&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;,
        &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Oviparity&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;lt;em&amp;gt;oviparous&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;, etc., we could create
        abstract classes for each of them. It would look like this:
        &amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
            abstract class Mammal extends Animal {
            }
            // ... other classes follow the same pattern
        &amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Speaking of &amp;lt;em&amp;gt;levels&amp;lt;/em&amp;gt; of abstraction; we should avoid creating too many levels of abstractions as then
        our code can become hard to reason about and difficult to maintain in the long term.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Abstract classes should be used when the business domain really demands it. We should not use them just
        because they give us a &amp;quot;free&amp;quot; pass to &amp;quot;code re-use&amp;quot;. If we don&amp;#39;t follow this recommendation we can end up with a
        lot of &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/No_Silver_Bullet&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;accidental complexity&amp;lt;/a&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Similarly, abstract classes should not be used with the purpose of preventing its instantiation. If that&amp;#39;s the
        goal, then the correct way of doing it is to declare a constructor for that class and make it
        &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt; (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.8.10&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;8.8.10
        &amp;lt;/a&amp;gt;).
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h2 id=&amp;quot;abstract-classes-final-thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Abstract classes represent a powerful tool at our disposal when following the OOP paradigm. Properly implemented,
    they enhance our code and make it more robust, secure, maintainable and profitable (why not?) in the long term. This
    article is by no means a complete guide to this topic, and there&amp;#39;s much more you can review on your own; but
    hopefully this is a good start for you to dive deeper into the documentation and information linked here.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Java annotation interfaces</title>
      <link>https://lealceldeiro.com/articles/2024/java-annotation-interfaces.html</link>
      <pubDate>Sat, 20 Jan 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/java-annotation-interfaces.html</guid>
      <description>
&amp;lt;h2 id=&amp;quot;what-s-a-java-interface&amp;quot;&amp;gt;What&amp;#39;s a Java annotation interface?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    In a &amp;lt;a href=&amp;quot;java-interfaces.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;previous article&amp;lt;/a&amp;gt; we learned about normal
    interfaces. This begs the question, then, what&amp;#39;s a Java annotation interface?
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    An &amp;lt;em&amp;gt;annotation interface&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.6&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;amp;sect;9.6
    &amp;lt;/a&amp;gt;) is an interface declared with a distinct syntax, intended to be implemented by reflective representations
    of &amp;lt;em&amp;gt;annotations&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.7&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;amp;sect;9.7
    &amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    We can say that it&amp;#39;s a specialized kind of interface and to differentiate it from a normal interface, the keyword
    &amp;lt;code&amp;gt;interface&amp;lt;/code&amp;gt; is preceded by an at sign (&amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;). For example:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
@interface TraceableMethod {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Once we have declared (or imported) an annotation interface in our code, we can use it to augment
    our application, for example, to enhance the Java type system with nullability information. An example of this
    is Spring&amp;#39;s
    &amp;lt;a href=&amp;quot;https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/lang/NonNull.html&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;@NonNull&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt; annotation.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    In this sense, annotations can be used as well with static code analysis tools to detect possible bugs in our
    code in an early stage and increase productivity in general by avoiding common mistakes in our code and focus in
    our business logic.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;syntax&amp;quot;&amp;gt;Syntax&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    From the previous example you&amp;#39;re probably already inferring the syntax of this type of interfaces. Let&amp;#39;s see it:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{InterfaceModifier} @ interface TypeIdentifier AnnotationInterfaceBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Where&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;{InterfaceModifier}&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is one of:
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-Annotation&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;Annotation&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;, &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt;,
        &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;strictfp&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt; is the special sing used to indicate that this is an annotation interface declaration, and not
        a normal interface declaration.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;interface&amp;lt;/code&amp;gt; is the Java keyword used to specify we&amp;#39;re declaring an interface.&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-TypeIdentifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;TypeIdentifier&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is the name we want to give the annotation. In our previous example we used &amp;lt;code&amp;gt;TraceableMethod&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-AnnotationInterfaceBody&amp;quot;
           target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;AnnotationInterfaceBody&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is the &amp;lt;code&amp;gt;body&amp;lt;/code&amp;gt; of our annotation, including the curly braces (&amp;lt;code&amp;gt;{}&amp;lt;/code&amp;gt;). See next section.
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h3 id=&amp;quot;syntax-body&amp;quot;&amp;gt;Body&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    The structure of the body of an annotation interface (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-AnnotationInterfaceMemberDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        AnnotationInterfaceMemberDeclaration
    &amp;lt;/a&amp;gt;) is as follows:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{
    AnnotationInterfaceElementDeclaration
    ConstantDeclaration
    ClassDeclaration
    InterfaceDeclaration
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s analyze
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-AnnotationInterfaceElementDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        AnnotationInterfaceElementDeclaration
    &amp;lt;/a&amp;gt;. The rest of the elements follow the same &amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt; rules as those described in
    &amp;lt;a href=&amp;quot;java-interfaces.html#interface-body&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;normal interface declarations&amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;h4 id=&amp;quot;elements&amp;quot;&amp;gt;Annotation interface elements&amp;lt;/h4&amp;gt;

&amp;lt;p&amp;gt;
    Annotations (like classes and interfaces) can have attributes. The difference is that they look like methods
    in the annotation body, even though they&amp;#39;re used as normal attributes where the annotation is used. Let&amp;#39;s see an
    example to get a better idea.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
@interface TraceableMethod {
    public String traceName() default &amp;quot;TracedMethod&amp;quot;;
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Here, we&amp;#39;ve defined an attribute (the method-like syntax) in the annotation declaration called
    &amp;lt;code&amp;gt;traceName&amp;lt;/code&amp;gt;. Notice how the syntax is somehow the same but different from fields declared in normal
    interfaces and classes. For example, the modifier (&amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;), the type (&amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;), and the
    identifier (&amp;lt;code&amp;gt;traceName&amp;lt;/code&amp;gt;) are specified similar to how they&amp;#39;d be specified in a class or interface, but
    then we have the parenthesis (&amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt;) and the &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; keyword used after it, along the actual
    attribute default value (&amp;lt;code&amp;gt;&amp;quot;TracedMethod&amp;quot;&amp;lt;/code&amp;gt;).
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;how-to-use-the-annotation&amp;quot;&amp;gt;How to use annotations?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Once the annotation is created (through the annotation interface declaration) it can be placed on classes, methods,
    fields, and a few other places, and it must appear immediately before what&amp;#39;s being annotated. For example, let&amp;#39;s
    suppose we have the following class.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Processor {
    void process() {
    }

    String getName() {
        return &amp;quot;processor&amp;quot;;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    Now, let&amp;#39;s suppose that the goal of the annotation we just declared (&amp;lt;code&amp;gt;TraceableMethod&amp;lt;/code&amp;gt;) is, given a
    class, to &amp;quot;mark&amp;quot; the methods in that class whose names we want to print when our program starts up. For this to
    happen we must do two things: first, &amp;lt;em&amp;gt;annotate&amp;lt;/em&amp;gt; the methods, and second, when our program starts up, &amp;quot;scan&amp;quot;
    our code and find these methods &amp;lt;em&amp;gt;annotated&amp;lt;/em&amp;gt; as &amp;lt;code&amp;gt;TraceableMethod&amp;lt;/code&amp;gt;. Let&amp;#39;s see it.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;annotating-an-element&amp;quot;&amp;gt;Annotate the methods&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;For now, we&amp;#39;re going to annotate only the &amp;lt;code&amp;gt;process&amp;lt;/code&amp;gt; method.&amp;lt;/p&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Processor {
    @TraceableMethod
    void process() {
    }

    String getName() {
        return &amp;quot;processor&amp;quot;;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h3 id=&amp;quot;find-elements-annotate-in-our-code&amp;quot;&amp;gt;Find the methods annotated in our code&amp;lt;/h3&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public static void main(String[] args) {
    Class&amp;lt;?&amp;gt; clazz = Processor.class;
    Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {
        for (Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof TraceableMethod traceableMethodAnnotation) {
                System.out.printf(&amp;quot;&amp;quot;&amp;quot;
                                  Method %s annotated as %s - trace name: %s
                                  &amp;quot;&amp;quot;&amp;quot;,
                                  method.getName(),
                                  TraceableMethod.class.getSimpleName(),
                                  traceableMethodAnnotation.traceName());
            }
        }
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    If you run this example you&amp;#39;ll see an empty output. That&amp;#39;s because we did not specify the
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/annotation/RetentionPolicy.html&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;
        retention policy
    &amp;lt;/a&amp;gt; of the annotation.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;There are three values we can use here:&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/annotation/RetentionPolicy.html#CLASS&amp;quot;
           target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;CLASS&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; (keep the annotation after the code is compiled but ignored by the JVM during runtime)
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/annotation/RetentionPolicy.html#RUNTIME&amp;quot;
           target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;RUNTIME&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; (keep the annotation after the code is compiled and by the JVM during runtime)
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/annotation/RetentionPolicy.html#SOURCE&amp;quot;
           target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;SOURCE&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; (discard the annotation by the compiler)
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;
    So, because &amp;lt;code&amp;gt;CLASS&amp;lt;/code&amp;gt; is the default retention policy our annotation was kept after compilation, but
    during runtime, it was not taken into account by the JVM, hence the call to &amp;lt;code&amp;gt;method.getAnnotations()&amp;lt;/code&amp;gt;
    returned an empty array. How do we fix it? We change the retention policy... by using a meta-annotation!
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
@Retention(RetentionPolicy.RUNTIME)
@interface TraceableMethod {
    public String traceName() default &amp;quot;TracedMethod&amp;quot;;
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    Now, if you run the same code again you&amp;#39;ll se the following output:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-shell&amp;quot;&amp;gt;
Method process annotated as TraceableMethod - trace name: TracedMethod
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h3 id=&amp;quot;specifying-attributes&amp;quot;&amp;gt;Specifying annotation attributes&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    What about the field &amp;lt;code&amp;gt;traceName&amp;lt;/code&amp;gt; declared in the body of the annotation? From the previous output
    we can see it took the default value (&amp;lt;code&amp;gt;TracedMethod&amp;lt;/code&amp;gt;); but what if we want to change it. Simple, we can
    specify it within parenthesis (&amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt;) after the annotation name.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Let&amp;#39;s annotate the method &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt;, in &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt;, and let&amp;#39;s specify for it a different
    &amp;lt;code&amp;gt;traceName&amp;lt;/code&amp;gt; value.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Processor {
    @TraceableMethod
    void process() {
    }

    @TraceableMethod(traceName = &amp;quot;String returning method&amp;quot;)
    String getName() {
        return &amp;quot;processor&amp;quot;;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    When we run again the code, now this is the output:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-shell&amp;quot;&amp;gt;
Method getName annotated as TraceableMethod - trace name: String returning method
Method process annotated as TraceableMethod - trace name: TracedMethod
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    As you see here, now the method &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; is reported with a &amp;lt;code&amp;gt;traceName&amp;lt;/code&amp;gt; value different from
    the default one, that&amp;#39;s it, &amp;lt;code&amp;gt;String returning method&amp;lt;/code&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;interface-examples&amp;quot;&amp;gt;Example: Using the &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; annotation&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    The previous example is contrived and somehow silly, the code doesn&amp;#39;t do anything useful. It was for demonstration
    purposes. Let&amp;#39;s see an example of an annotation that&amp;#39;s used very often and which is extremely helpful:
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Override.html&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        @Override
    &amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This annotation provides us with great benefit during compile-time by &amp;quot;forcing&amp;quot; us to properly implement an
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        equals
    &amp;lt;/a&amp;gt; method in our class.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Consider the common error made when overriding, shown in the next code snippet:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Processor {
    public boolean equals(Processor obj) {
        // return true or false ... omitted for brevity
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    In this code there&amp;#39;s one subtle mistake: the argument received in the &amp;lt;code&amp;gt;equals&amp;lt;/code&amp;gt; method is of type
    &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt;, but it should be of type &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt; as per the specs. However, we may not notice
    it. And the compiler can&amp;#39;t help us: it&amp;#39;s a completely valid java code; in fact this code creates an accidental
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        overload
    &amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; to the rescue! If we annotate the method with &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt;, now the compiler knows
    we&amp;#39;re intending to override the &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;&amp;#39;s &amp;lt;code&amp;gt;equals&amp;lt;/code&amp;gt; method and reports an error at
    compile time (Something like &amp;lt;em&amp;gt;&amp;quot;Method does not override method from its superclass&amp;quot;&amp;lt;/em&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    This would be the fixed version of the previous code:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
@Override
public boolean equals(Object obj) {
    // code omitted for brevity
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    It&amp;#39;s worth mentioning that we&amp;#39;re not limited to the &amp;lt;code&amp;gt;equals&amp;lt;/code&amp;gt; method. We can use this annotation with
    any method we intend to override from some of the class ancestors.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;why-do-we-need-interfaces&amp;quot;&amp;gt;Do we need annotation interfaces?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Technically speaking, we may not strictly need them to write our day to day software, but it&amp;#39;s really difficult
    for me to imagine any modern Java program that doesn&amp;#39;t use some form of annotation. They enhance our code in ways
    that makes our lives (as developers) much easier than if we had to do the same work programmatically.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Annotations are also used to provide metadata at runtime. For example, the Jakarta Persistence API (
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Jakarta_Persistence&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        JPA
    &amp;lt;/a&amp;gt;) uses several annotations from the package &amp;lt;code&amp;gt;jakarta.persistence&amp;lt;/code&amp;gt; to work internally and provide
    all the features it provides. So, it&amp;#39;s not only small enhancements we get in our code; we&amp;#39;re talking at this point
    of entire frameworks relying on annotations to function properly. This is something I don&amp;#39;t see going away any time
    soon.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;final-thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    There&amp;#39;s a bunch of articles, official documentation, and books out there that we can read to get more knowledge
    about annotations. Hopefully, this is a good start for you to increase your knowledge a bit more about the Java
    platform and sparkle your interest in reading more about interface annotations and other use cases and details
    not covered in this article.
&amp;lt;/p&amp;gt;

&amp;lt;hr/&amp;gt;

&amp;lt;p&amp;gt;
    &amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;: While the structure and most of the &amp;quot;rules&amp;quot; are the same, there are actually some subtle differences.
    For example, the return type of a method declared in the body of annotation interface must be one of: a primitive
    type, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Class&amp;lt;/code&amp;gt; or an invocation of &amp;lt;code&amp;gt;Class&amp;lt;/code&amp;gt;, an enum class type, an
    annotation interface type, or an array type whose component type is one of the previously mentioned types. If such
    return type is different, a compile-time error occurs.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Java interfaces</title>
      <link>https://lealceldeiro.com/articles/2024/java-interfaces.html</link>
      <pubDate>Wed, 17 Jan 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/java-interfaces.html</guid>
      <description>
&amp;lt;h2 id=&amp;quot;what-s-a-java-interface&amp;quot;&amp;gt;What&amp;#39;s a Java interface?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    If you ask someone who&amp;#39;s not a programmer, or software engineer, or the like, &amp;quot;what&amp;#39;s an interface?&amp;quot;, most probably
    you&amp;#39;ll get answers around &amp;quot;it&amp;#39;s a boundary where two entities interact&amp;quot;, &amp;quot;it&amp;#39;s some sort of connection between two
    things through which they interact with each other&amp;quot;, or &amp;quot;it&amp;#39;s the means through which two unrelated elements
    interact with each other&amp;quot;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Then, what&amp;#39;s a Java interface?&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Interfaces in Java define behaviors. These behaviors are expressed as a
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/concepts/interface.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;group of methods&amp;lt;/a&amp;gt;
    in the body of the interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Then, some
    &amp;lt;a href=&amp;quot;https://lealceldeiro.com/articles/2024/java-oop-classes-and-objects.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;class&amp;lt;/a&amp;gt;
    can implement the interface methods as it sees fit, that&amp;#39;s it, it defines how the behaviors specified in the
    interface are actually implemented. This class would be the first &amp;quot;entity&amp;quot; from our previous wider definition of
    interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    The second &amp;quot;entity&amp;quot;, the one that would interact with the class implementation through the interface,
    would be the caller: the one that &amp;lt;em&amp;gt;calls&amp;lt;/em&amp;gt; the methods.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    In summary we could say: the interface defines what behaviors/methods it provides to callers, and
    that must be implemented by any concrete class. It establishes a well-defined
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/API&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;API&amp;lt;/a&amp;gt; contract between two parties: one party that
    provides the implementation and another party that will consume it.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    If we were to create a simulation of a restaurant to illustrate the usage of an interface. We could have three
    elements:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;A customer&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;A waiter&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;The interface, through which the customer asks the waiter to bring some food&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;p&amp;gt;
    We could call these three elements like this, respectively:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; (class)&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;Waiter&amp;lt;/code&amp;gt; (another class)&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; (the interface)&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h2 id=&amp;quot;interface-declaration-in-java&amp;quot;&amp;gt;How does an interface declaration look like in Java?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s look at an example, by following our previous analogy of the restaurant:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
// interface declaration
interface OrderProcessor {
    // here we declare only the method (behavior) - but not the actual implementation
    public void bringFood(String dishName);
}

// a class that implements the interface
class Waiter implements OrderProcessor {
    // the actual logic of the method (body of the method) is defined here
  	public void bringFood(String dishName) {
	    System.out.println(&amp;quot;Brought &amp;quot; + dishName + &amp;quot; to customer&amp;quot;);
    }
}

// another class that can ask for food
class Customer {
    private String favoriteDish = &amp;quot;Fish and Vegetables&amp;quot;;

    void askForFood(OrderProcessor orderProcessor) {
	    // the customer interacts with the interface OrderProcessor
        orderProcessor.bringFood(favoriteDish);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    This interface is called &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; and it defines the &amp;quot;bring food&amp;quot; behavior, which is declared as
    a method named: &amp;lt;code&amp;gt;bringFood&amp;lt;/code&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;This is a very simple example; let&amp;#39;s dive deeper into the intricacies of interfaces in Java.&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;diving-deeper-into-java-interfaces&amp;quot;&amp;gt;Diving deeper into Java interfaces&amp;lt;/h2&amp;gt;
&amp;lt;h3 id=&amp;quot;declaring-an-interface&amp;quot;&amp;gt;Declaring an interface&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;There are two types of interface declarations:&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;em&amp;gt;Normal interface declarations&amp;lt;/em&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;em&amp;gt;Annotation interface declarations&amp;lt;/em&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;p&amp;gt;
    In this article only the normal interface declaration is covered. Annotation interface declaration will be covered
    in a separate article.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s see the structure of an interface declaration:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{InterfaceModifier} interface TypeIdentifier [TypeParameters] [InterfaceExtends] [InterfacePermits] InterfaceBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Where&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;{InterfaceModifier}&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is one of:
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-Annotation&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;Annotation&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;, &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt;,
        &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;strictfp&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;interface&amp;lt;/code&amp;gt; is the Java keyword used to specify we&amp;#39;re declaring an interface.&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-TypeIdentifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;TypeIdentifier&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is the name we want to give the interface. In our previous example we used &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-TypeParameters&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[TypeParameters]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is a collection of &amp;lt;em&amp;gt;types&amp;lt;/em&amp;gt; we can define within angle brackets (&amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;gt;&amp;lt;/code&amp;gt;).
        For instance,&amp;lt;code&amp;gt;&amp;amp;lt;T&amp;amp;gt;&amp;lt;/code&amp;gt;. See
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/extra/generics/index.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            generics
        &amp;lt;/a&amp;gt; and
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.2&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;8.1.1
        &amp;lt;/a&amp;gt; for more info about this.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceExtends&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[InterfaceExtends]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is the portion of the interface declaration where we can specify, after the keyword &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt;,
        the interface (if any) from which our interface is a direct subinterface. For instance,
        &amp;lt;code&amp;gt;extends Processor&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfacePermits&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[InterfacePermits]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; is used when the &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; modifier is specified, and it indicates, after the keyword
        &amp;lt;code&amp;gt;permits&amp;lt;/code&amp;gt;, the other classes or interfaces (if any) that are allowed to implement/extend our
        interface. For example, &amp;lt;code&amp;gt;permits OrderProcessor, SuperWaiter&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceBody&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;InterfaceBody&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is the &amp;lt;code&amp;gt;body&amp;lt;/code&amp;gt; of our interface, including the curly braces (&amp;lt;code&amp;gt;{}&amp;lt;/code&amp;gt;). See next section.
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;
    It&amp;#39;s important to note that unlike a class, an interface cannot be declared &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;. However, an
    interface may be declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.1.1.4&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;9.1.1.4
    &amp;lt;/a&amp;gt;) to limit its subclasses and subinterfaces.
&amp;lt;/p&amp;gt;

&amp;lt;h4 id=&amp;quot;interface-body&amp;quot;&amp;gt;Interface body&amp;lt;/h4&amp;gt;

&amp;lt;p&amp;gt;
    The structure of an interface body (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceMemberDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;{InterfaceMemberDeclaration}&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;) is as follows:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{
    ConstantDeclaration
	InterfaceMethodDeclaration
	ClassDeclaration
	InterfaceDeclaration
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s see what each of these elements are, and some examples of how they&amp;#39;re used.&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;interface-constants&amp;quot;&amp;gt;Constants&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;
    Constants (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-ConstantDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;ConstantDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;) can be referred to as interface attributes, properties, fields. They are similar to class fields, except they
    can NOT be modified &amp;amp;mdash;they&amp;#39;re final&amp;amp;mdash;, and their declaration is as follows:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{ConstantModifier} UnannType VariableDeclaratorList;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;In this case&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-ConstantModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;ConstantModifier&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt; can be any of the previously mentioned &amp;lt;code&amp;gt;Annotation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;,
        and/or &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-UnannType&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;UnannType&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;, in short, refers to all the types we can use to declare what type the field is. For example,
        &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-VariableDeclaratorList&amp;quot;
           target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;VariableDeclaratorList&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;, simply put, is the actual name of the field (variable) and, optionally, and initializer.
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s see an example of a constant in our &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; interface.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    public int MAX_ORDERS_NUMBER = 1000;
    // ... rest of the code omitted for brevity
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Here, we declared a constant &amp;lt;code&amp;gt;MAX_ORDERS_NUMBER&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; with a value of
    &amp;lt;code&amp;gt;1000&amp;lt;/code&amp;gt;. It is &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt; (default in interfaces), so it&amp;#39;s publicly accessible by anyone with
    access to the interface.
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;interface-method-declarations&amp;quot;&amp;gt;Method declarations&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;
    Method declarations (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceMethodDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;InterfaceMethodDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;) are the declarations of the methods defined in the interface. They
    are the methods that must be implemented by any class that implements the interface, unless the interface itself
    provides a default implementation.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Default method implementations is a feature added in
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html#javase8&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        Java 8
    &amp;lt;/a&amp;gt; that allows us to define the method body (its actual implementation) in the interface itself, as opposite to
    abstract methods, which require a concrete class to implement them.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    More formally, a &amp;lt;em&amp;gt;default method&amp;lt;/em&amp;gt; is an instance method declared in an interface with the
    &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; modifier. Its body is always represented by a block, which provides a default implementation
    for any class that implements the interface without overriding the method. These methods are distinct from concrete
    methods, which are declared in classes, and from &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt; interface methods, which are neither
    inherited nor overridden (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.4&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;9.4
    &amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Let&amp;#39;s see now how to declare these methods in our &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; interface.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    public int MAX_ORDERS_NUMBER = 1000;

    // abstract method -- must be implemented by the concrete class
    public void bringFood(String dishName);

    // also, an abstract method -- must be implemented by the concrete class
    public abstract void bringWater();

    // any class implementing the interface will inherit this
    // default method implementation, unless overridden
    default String getType() {
        return &amp;quot;Waiter&amp;quot;;
    }

    // only available to be called within the interface itself
    private int getMaxOrders() {
        return MAX_ORDERS_NUMBER;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5 id=&amp;quot;interface-nested-class-declaration&amp;quot;&amp;gt;Class declaration&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;
    Nested (or inner) class declarations (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;ClassDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;) were covered in a previous post (
    &amp;lt;a href=&amp;quot;https://lealceldeiro.com/articles/2024/java-oop-classes-and-objects.html#nested-classes&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;
        Java classes and objects
    &amp;lt;/a&amp;gt;). Let&amp;#39;s quickly review an example of how it would look like in the body of an interface:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    // nested class declaration
    class BillCalculator {
    }
    // ... rest of the code omitted for brevity
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5 id=&amp;quot;interface-inner-interface-declaration&amp;quot;&amp;gt;Interface Declaration&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;
    Nested interface declarations (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;InterfaceDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;) were also covered in
    &amp;lt;a href=&amp;quot;https://lealceldeiro.com/articles/2024/java-oop-classes-and-objects.html#nested-interfaces&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;
        that post
    &amp;lt;/a&amp;gt;. Let&amp;#39;s see how to use it in our example.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    interface Calculator {
    }
    // ... rest of the code omitted for brevity
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h3 id=&amp;quot;implementing-an-interface&amp;quot;&amp;gt;Implementing an interface&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;
    An interface is not of much use without a concrete class that implements its methods. So, let&amp;#39;s get into how a class
    can implement the methods defined in the body of an interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    A class can be declared to &amp;lt;em&amp;gt;directly implement&amp;lt;/em&amp;gt; one or more interfaces (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;9&amp;lt;/a&amp;gt;,
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.5&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;8.1.5
    &amp;lt;/a&amp;gt;). This means that any instance of that class implements all the &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; methods specified by
    the interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    A class necessarily implements all the interfaces that its direct superclasses and direct superinterfaces do. This
    multiple interface inheritance allows objects to support multiple common behaviors without sharing a superclass
    (&amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;9&amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s review again our previous example.&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    public void bringFood(String dishName);
}

class Waiter implements OrderProcessor {
  	public void bringFood(String dishName) {
        System.out.println(&amp;quot;Brought &amp;quot; + dishName + &amp;quot; to customer&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    Here, we specify after the keyword &amp;lt;code&amp;gt;implements&amp;lt;/code&amp;gt; all the interfaces that the class implements. Notice that
    it&amp;#39;s mandatory for the &amp;lt;code&amp;gt;Waiter&amp;lt;/code&amp;gt; class to implement the method &amp;lt;code&amp;gt;bringFood&amp;lt;/code&amp;gt;, otherwise the
    code won&amp;#39;t compile.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Let&amp;#39;s see how the &amp;lt;code&amp;gt;Waiter&amp;lt;/code&amp;gt; class can implement another interface: &amp;lt;code&amp;gt;Serializable&amp;lt;/code&amp;gt;, which
    belongs to the &amp;lt;code&amp;gt;java.io&amp;lt;/code&amp;gt; package.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Waiter implements OrderProcessor, Serializable {
  	public void bringFood(String dishName) {
	      System.out.println(&amp;quot;Brought &amp;quot; + dishName + &amp;quot; to customer&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    As you notice, we only need to add the new interface being implemented after a comma (&amp;lt;code&amp;gt;,&amp;lt;/code&amp;gt;). This way we
    can implement all the interfaces we want.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;extending-an-interface&amp;quot;&amp;gt;Extending an interface&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    Sometimes there are interfaces that do not provide all the method declarations we need. In such cases, sometimes we
    need to create an interface that &amp;quot;inherits&amp;quot; that other interface method declarations, and, on top of that, add our
    own. This way any class implementing the new interface will inherit all the methods. This is a
    &amp;lt;a href=&amp;quot;https://dzone.com/articles/introduction-spring-data-jpa-inheritance-rest-app&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        common pattern used
    &amp;lt;/a&amp;gt; when implementing our own
    &amp;lt;a href=&amp;quot;https://spring.io/projects/spring-data-jpa/&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;Spring Data JPA&amp;lt;/a&amp;gt; Repositories.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    An interface may be declared to be a &amp;lt;em&amp;gt;direct extension&amp;lt;/em&amp;gt; of one or more other interfaces, meaning that it
    inherits all the member classes and interfaces, instance methods, and &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; fields of the interfaces
    it extends, except for any members that it may override or hide (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;9&amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s see in our example how would such extension look like.&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface Processor&amp;amp;lt;T&amp;amp;gt; {                              // 1
	public void process(T element);                   // 2
}

interface OrderProcessor extends Processor&amp;amp;lt;String&amp;amp;gt; {  // 3
    public void bringFood(String dishName);

	public default void process(String dishName) {    // 4
	    bringFood(dishName);
	}
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    There are several things going on in this example, so let&amp;#39;s review it, point by point from the previous commented
    lines:
&amp;lt;/p&amp;gt;

&amp;lt;ol&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt; is declared as a generic interface that accepts a type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;
        (this is the type of the elements the interface will work with). See
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.1.2&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;9.1.2
        &amp;lt;/a&amp;gt; for more information about this.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt; declares an abstract method called &amp;lt;code&amp;gt;process&amp;lt;/code&amp;gt;, which takes an argument of type
        &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; extends &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt; and it specifies that the
        elements it will work with are of type &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;OrderProcessor&amp;lt;/code&amp;gt; provides a default implementation for the &amp;lt;code&amp;gt;process&amp;lt;/code&amp;gt; method, inherited
        from &amp;lt;code&amp;gt;Processor&amp;lt;/code&amp;gt;. The default implementation is actually just a call to the abstract method
        &amp;lt;code&amp;gt;bringFood&amp;lt;/code&amp;gt; and it will be inherited by any concrete class.
    &amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;p&amp;gt;This example shows how powerful our implementations can be by combining different Java features.&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;evolving-interfaces&amp;quot;&amp;gt;Evolving interfaces&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    When an interface is defined and made available to client code (let it be our own code, library users, or third
    party vendors who rely on our interfaces definitions), it&amp;#39;s extremely important to be careful with how we evolve it
    so we don&amp;#39;t break existing code that use the current API contract defined by the interface.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    While this is a topic for another whole article. There are some important points we should touch upon here. The
    following points are an extract from
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-13.html#jls-13.5&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;amp;sect;13.5
    &amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        If an interface that is declared &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt; is changed to not be declared &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, then
        an &amp;lt;code&amp;gt;IllegalAccessError&amp;lt;/code&amp;gt; is thrown if a pre-existing binary is linked that needs but no longer has
        access to the interface type, so such a change is not recommended for widely distributed interfaces.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        If an interface that was freely extensible (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.1.1.4&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;9.1.1.4
        &amp;lt;/a&amp;gt;) is changed to be declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;, then an &amp;lt;code&amp;gt;IncompatibleClassChangeError&amp;lt;/code&amp;gt; is thrown
        if a binary of a pre-existing subclass or subinterface of this interface is loaded and is not a permitted direct
        subclass or subinterface of this interface (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.1.4&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;9.1.4
        &amp;lt;/a&amp;gt;); such a change is also not recommended for widely distributed classes.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Adding an interface to the set of permitted direct subinterfaces of a &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; interface will not
        break compatibility with pre-existing binaries. However, it may cause the execution of an exhaustive
        &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.1&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            &amp;amp;sect;14.11.1
        &amp;lt;/a&amp;gt;) to
        fail with an error (a &amp;lt;code&amp;gt;MatchException&amp;lt;/code&amp;gt; may be thrown) if the &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; encounters
        an instance of the new permitted direct subinterface that was not known at compile time (
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.3&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;14.11.3&amp;lt;/a&amp;gt;,
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.28.2&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;15.28.2&amp;lt;/a&amp;gt;).
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        If a class or interface is removed from the set of permitted direct subclasses or subinterfaces of a
        &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; interface, then an &amp;lt;code&amp;gt;IncompatibleClassChangeError&amp;lt;/code&amp;gt; is thrown if the pre-existing
        binary of the removed class or interface is loaded.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Changing an interface that was declared &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; to be declared &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt; does not
        break compatibility with pre-existing binaries. However, a &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt; interface &amp;lt;em&amp;gt;X&amp;lt;/em&amp;gt; must
        have a &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; direct superinterface. Deleting the &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt; modifier would prevent
        &amp;lt;em&amp;gt;X&amp;lt;/em&amp;gt; from being recompiled, as every interface with a &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; direct superinterface must
        be &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;Deleting a member from an interface may cause linkage errors in pre-existing binaries.&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h3 id=&amp;quot;interface-examples&amp;quot;&amp;gt;Examples&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s see some more examples.&amp;lt;/p&amp;gt;

&amp;lt;h4 id=&amp;quot;example1&amp;quot;&amp;gt;A public generic interface with two constants, one private method and one public method&amp;lt;/h4&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public interface Processor&amp;amp;lt;T&amp;amp;gt; {
    int MAX_PROCESSES = 5;
    boolean LANGUAGE_AGNOSTIC = true;

	public void process(T element);

    private boolean isValid(T e) {
        return e != null;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h4 id=&amp;quot;example2&amp;quot;&amp;gt;
    A public interface with one static inner class and one static inner interface; both of them have
    package-private visibility
&amp;lt;/h4&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public interface OrderProcessor {
    class Sender {                  // &amp;amp;lt;-- the &amp;#39;static&amp;#39; keyword is default here before the keyword &amp;#39;class&amp;#39;
    }
    static interface Calculator {
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h4 id=&amp;quot;example3&amp;quot;&amp;gt;A package-private interface with one public static nested interface&amp;lt;/h4&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface OrderProcessor {
    public static interface Calculator {
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h4 id=&amp;quot;example5&amp;quot;&amp;gt;
    A sealed interface that permits another sealed interface to extend it, and a final class that implements the
    intermediate interface
&amp;lt;/h4&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed interface Processor&amp;amp;lt;T&amp;amp;gt; permits OrderProcessor {
    void process(T element);
}
sealed interface OrderProcessor extends Processor&amp;amp;lt;String&amp;amp;gt; {
    void process(String order);
}
final class Waiter implements OrderProcessor {
  	public void process(String order) {
	    System.out.println(&amp;quot;Brought &amp;quot; + order + &amp;quot; to customer&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h4 id=&amp;quot;example6&amp;quot;&amp;gt;
    A sealed interface that permits another non-sealed interface to extend it, and a NON-final class that implements
    the intermediate interface
&amp;lt;/h4&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed interface Processor&amp;amp;lt;T&amp;amp;gt; permits OrderProcessor {
    void process(T element);
}
non-sealed interface OrderProcessor extends Processor&amp;amp;lt;String&amp;amp;gt; {
    void process(String order);
}
class Waiter implements OrderProcessor {
  	public void process(String order) {
	    System.out.println(&amp;quot;Brought &amp;quot; + order + &amp;quot; to customer&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h4 id=&amp;quot;example7&amp;quot;&amp;gt;
    A sealed interface that permits a final class to implement it
&amp;lt;/h4&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed interface OrderProcessor permits Waiter {
    void process(String order);
}
final class Waiter implements OrderProcessor {
  	public void process(String order) {
	    System.out.println(&amp;quot;Brought &amp;quot; + order + &amp;quot; to customer&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h3 id=&amp;quot;why-do-we-need-interfaces&amp;quot;&amp;gt;Why do we need interfaces?&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    While interfaces are not a mandatory component of the software we implement, it&amp;#39;s true our programs can use
    interfaces to provide a common supertype for otherwise unrelated classes, and to make it unnecessary for related
    classes to share a common &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt; superclass (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;amp;sect;9&amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Many software patterns have been put into practice taking into account the concept of interfaces. These patterns
    solve well-known problems with a defined context and solution. Just to mention a few, we could point out
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/SOLID&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;SOLID&amp;lt;/a&amp;gt;, and many of the patterns presented by
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/Design_Patterns&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;GoF&amp;lt;/a&amp;gt;; thus they bring flexibility,
    robustness, re-usability and a lot more benefits to our well-designed software solutions.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Nowadays, when systems are more complex, and we need to model more complex scenarios as well, interfaces
    play a vital role in promoting a loose coupling between the elements of our software. They&amp;#39;re also a very powerful
    element of Java: they allow a class to inherit behaviors from multiple supertypes (so we can overcome the
    &amp;quot;limitation&amp;quot; of not having
    &amp;lt;a href=&amp;quot;https://medium.com/edureka/multiple-inheritance-in-java-a996c26143ac&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        multiple inheritance in Java
    &amp;lt;/a&amp;gt;), they play a pivotal role in
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        polymorphism
    &amp;lt;/a&amp;gt;, they allow us to define clear boundaries and responsibilities for each software component and in most cases
    they make easier for us to implement automated test for the code we write.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;final-thoughts&amp;quot;&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    There is so much material we could revise about Java interfaces that this single article wouldn&amp;#39;t be enough to cover
    it all. I&amp;#39;ve presented here high level information as well as several of the implementation details about this topic
    which I hope, along with the linked documentation, is a good start for you to review and deepen your knowledge about
    Java.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>Java classes and objects</title>
      <link>https://lealceldeiro.com/articles/2024/java-oop-classes-and-objects.html</link>
      <pubDate>Wed, 10 Jan 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/java-oop-classes-and-objects.html</guid>
      <description>
&amp;lt;h2 id=&amp;quot;what-are-java-classes&amp;quot;&amp;gt;What are Java classes and objects?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    To put it simple, a &amp;lt;strong&amp;gt;Java class&amp;lt;/strong&amp;gt; is a &amp;quot;form of classification&amp;quot;. It is a concept from
    &amp;lt;em&amp;gt;Object-Oriented Programming (OOP)&amp;lt;/em&amp;gt; that allows us to &amp;quot;classify&amp;quot; (or group together) a type of
    &amp;lt;em&amp;gt;object(s)&amp;lt;/em&amp;gt; &amp;amp;mdash;another concept from OOP&amp;amp;mdash; An &amp;lt;strong&amp;gt;object&amp;lt;/strong&amp;gt; represents a specific instance
    of a class. With classes and objects we can represent real-world concepts and entities in our programs as well as
    their interactions.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    For instance, in a program called &amp;lt;em&amp;gt;ZooManager&amp;lt;/em&amp;gt; we could have the following classes: &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;,
    &amp;lt;code&amp;gt;Cage&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Food&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;ZooKeeper&amp;lt;/code&amp;gt;. Likewise, the specific animal, cage, food and
    zookeeper &amp;lt;em&amp;gt;instances&amp;lt;/em&amp;gt; that are present in the Zoo we&amp;#39;re managing would be the objects instantiated during
    the execution of the program. In this case, examples of instances would be &amp;lt;code&amp;gt;teo&amp;lt;/code&amp;gt; (a tiger),
    &amp;lt;code&amp;gt;cage1&amp;lt;/code&amp;gt; (a cage), &amp;lt;code&amp;gt;meat&amp;lt;/code&amp;gt; (food), and &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; (a zookeeper).
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;how-to-decide-which-classes-to-define&amp;quot;&amp;gt;How do we decide which classes to define in our program?&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    Classes define common &amp;lt;em&amp;gt;properties&amp;lt;/em&amp;gt; and &amp;lt;em&amp;gt;behaviors&amp;lt;/em&amp;gt; for the objects they represent. For example, the
    &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class would have the following properties:
&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;energy&amp;lt;/code&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;and the following behaviors&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;move&amp;lt;/code&amp;gt; (if an animal moves, it consumes energy, the &amp;lt;code&amp;gt;energy&amp;lt;/code&amp;gt; property decreases)
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;code&amp;gt;eat&amp;lt;/code&amp;gt; (if an animal eats, it recovers energy, the &amp;lt;code&amp;gt;energy&amp;lt;/code&amp;gt; property increases)
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;
    So, when we identify a collection of objects in our business domain that share the same properties and behaviors,
    we are usually in the presence of a class to be defined in our program. Sometimes it can be obvious, like
    knowing we&amp;#39;ll need a class called &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; in our ZooManager program, but sometimes it&amp;#39;s more difficult
    to identify when we would benefit from the definition of a class. As we gain experience throughout the years, as
    developers/software engineers, it becomes easier to identify each case.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;syntax&amp;quot;&amp;gt;Syntax&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    The simplest form of a class definition in Java you could find is this (&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; could have been any
    other name we define):
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    But that class isn&amp;#39;t very useful, is it? So, let&amp;#39;s see everything we have in our power to declare and define our
    own classes.
&amp;lt;/p&amp;gt;

&amp;lt;h3 id=&amp;quot;structure&amp;quot;&amp;gt;Structure&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s use the chapter about classes from the
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        Java Language Specification (JLS) for Java 21
    &amp;lt;/a&amp;gt;
    to understand the anatomy of a class in Java.
    Notice you may want to check the
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;JLS specific to your Java version&amp;lt;/a&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;Formally, this is the structure of a Java class:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{ClassModifier} class TypeIdentifier [TypeParameters] [ClassExtends] [ClassImplements] [ClassPermits] ClassBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;Where&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassModifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;{ClassModifier}&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is one of: &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;,
        &amp;lt;a href=&amp;quot;/articles/2024/abstrat-classes-in-java.html&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;abstract&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;, &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt;,
        &amp;lt;a href=&amp;quot;/articles/2024/sealed-classes-in-java.html#sealed-keyword&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;,
        &amp;lt;code&amp;gt;non-sealed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;strictfp&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;class&amp;lt;/code&amp;gt; is the Java keyword used to specify we&amp;#39;re declaring a class.&amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-TypeIdentifier&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;TypeIdentifier&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is the name we want to give the class. In our previous example we used &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        &amp;lt;!--https://en.wikipedia.org/wiki/List_of_typographical_symbols_and_punctuation_marks--&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-TypeParameters&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[TypeParameters]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is a collection of &amp;lt;em&amp;gt;types&amp;lt;/em&amp;gt; we can define within angle brackets (&amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;gt;&amp;lt;/code&amp;gt;). For instance,
        &amp;lt;code&amp;gt;&amp;amp;lt;T&amp;amp;gt;&amp;lt;/code&amp;gt;. See
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/extra/generics/index.html&amp;quot;
           target=&amp;quot;_blank&amp;quot;&amp;gt;&amp;lt;em&amp;gt;generics&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;
        and &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.1.2&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;JLS&amp;lt;/a&amp;gt;
        for more info about this.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassExtends&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[ClassExtends]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is the portion of the class declaration where we can specify, after the keyword &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt;, the class
        (if any) from which our class is a direct child (descendant, inheritor). For instance,
        &amp;lt;code&amp;gt;extends LivingBeing&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassImplements&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[ClassImplements]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is the portion of the class declaration where we can list, after the keyword &amp;lt;code&amp;gt;implements&amp;lt;/code&amp;gt;, the
        &amp;lt;a href=&amp;quot;https://lealceldeiro.com/articles/2024/java-interfaces.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
            interfaces
        &amp;lt;/a&amp;gt;
        (if any) whose behavior our class implements. For instance, &amp;lt;code&amp;gt;implements MovingBeing, Eater&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassPermits&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;[ClassPermits]&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        is used when the
        &amp;lt;a href=&amp;quot;/articles/2024/sealed-classes-in-java.html#sealed-keyword&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;sealed&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;
        modifier is specified, and it indicates, after the keyword
        &amp;lt;a href=&amp;quot;/articles/2024/sealed-classes-in-java.html#permits-keyword&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;permits&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;,
        the other classes (if any) that are allowed to extend our class. For example,
        &amp;lt;code&amp;gt;permits Tiger, Ostrich, Bear&amp;lt;/code&amp;gt;.
    &amp;lt;/li&amp;gt;

    &amp;lt;li&amp;gt;
        and,
        &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassBody&amp;quot; target=&amp;quot;_blank&amp;quot;
           class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
            &amp;lt;code&amp;gt;ClassBody&amp;lt;/code&amp;gt;
        &amp;lt;/a&amp;gt;,
        &amp;lt;!--https://www.compart.com/en/unicode/html--&amp;gt;
        is the &amp;lt;em&amp;gt;body&amp;lt;/em&amp;gt; of our class, including the curly braces (&amp;amp;lcub;&amp;amp;rcub;). See next section.
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;h4 id=&amp;quot;class-body&amp;quot;&amp;gt;Class body&amp;lt;/h4&amp;gt;

&amp;lt;p&amp;gt;
    In short, this is the structure of the class body (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassBodyDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;{ClassBodyDeclaration}&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;):
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
{
    ClassMemberDeclaration
    InstanceInitializer
    StaticInitializer
    ConstructorDeclaration
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s see what these elements are, along with some examples.
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;members&amp;quot;&amp;gt;Members&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;
    With &amp;lt;em&amp;gt;members&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassMemberDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;ClassMemberDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ), we refer to &amp;lt;em&amp;gt;fields&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-FieldDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;FieldDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ), &amp;lt;em&amp;gt;methods&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-MethodDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;MethodDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ), &amp;lt;em&amp;gt;nested classes&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ClassDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;ClassDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ), and &amp;lt;em&amp;gt;nested interfaces&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-InterfaceDeclaration&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;InterfaceDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ).
    The first two are the respective &amp;lt;em&amp;gt;properties&amp;lt;/em&amp;gt; and &amp;lt;em&amp;gt;behaviors&amp;lt;/em&amp;gt; that we were referring to initially.
    The other two are covered in next sections, along with &amp;lt;code&amp;gt;InstanceInitializer&amp;lt;/code&amp;gt; and
    &amp;lt;code&amp;gt;StaticInitializer&amp;lt;/code&amp;gt;.

&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s see fields and methods.&amp;lt;/p&amp;gt;

&amp;lt;h6 id=&amp;quot;fields&amp;quot;&amp;gt;Fields&amp;lt;/h6&amp;gt;

&amp;lt;p&amp;gt;
    Fields are properties, attributes that characterize our classes, they hold the state of the objects created from
    our classes and are useful for storing the objects information during runtime.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    We saw already some examples of properties for our mentioned &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class. Now, let&amp;#39;s see how we can
    actually write that in our code. Let&amp;#39;s enhance our &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class with some fields:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    String name;
    int energy;
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h6 id=&amp;quot;methods&amp;quot;&amp;gt;Methods&amp;lt;/h6&amp;gt;

&amp;lt;p&amp;gt;
    Methods define the behavior of the objects, they specify what &amp;quot;actions&amp;quot; the objects can do during the execution
    of our program, they define the
    &amp;lt;a href=&amp;quot;https://en.wikipedia.org/wiki/API&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        API
    &amp;lt;/a&amp;gt;
    through which we (technically speaking, other objects) can interact with them. They can do two things: return
    information about the object, and/or modify it, that&amp;#39;s it, modify the internal state of the object.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s see how we can define the actions our &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; objects can do:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    String name;
    int energy;

    void move() {
        energy--;
        System.out.println(name + &amp;quot; moved&amp;quot;);
    }

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    In this example, the method &amp;lt;code&amp;gt;move&amp;lt;/code&amp;gt; decreases the &amp;lt;em&amp;gt;energy&amp;lt;/em&amp;gt; value and prints the name of the animal
    that moved (i.e.: &amp;lt;code&amp;gt;teo moved&amp;lt;/code&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    The method &amp;lt;code&amp;gt;eat&amp;lt;/code&amp;gt; does the opposite: it increases the &amp;lt;em&amp;gt;energy&amp;lt;/em&amp;gt; value, and also prints the name of
    the animal that ate (i.e.: &amp;lt;code&amp;gt;teo ate&amp;lt;/code&amp;gt;).
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;constructors&amp;quot;&amp;gt;Constructors&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;So far, we&amp;#39;ve talked a lot about Java classes, but not so much about the objects.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Just to make sure we understand the concept of &amp;lt;em&amp;gt;object&amp;lt;/em&amp;gt;, if we were to make an analogy, we could say that
    &amp;lt;em&amp;gt;Woman&amp;lt;/em&amp;gt; is a class, while &amp;lt;em&amp;gt;Ana&amp;lt;/em&amp;gt; is an object. Similarly, &amp;lt;em&amp;gt;Man&amp;lt;/em&amp;gt; is a class, and &amp;lt;em&amp;gt;John&amp;lt;/em&amp;gt;
    is an object.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Now, let&amp;#39;s see how we can create objects in our programs. Java provides us with something called
    &amp;lt;em&amp;gt;constructors&amp;lt;/em&amp;gt; (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-ConstructorDeclaration&amp;quot;
       target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;ConstructorDeclaration&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ). They are a &amp;quot;special&amp;quot; piece of code inside the body of our class that allow us to &amp;lt;em&amp;gt;instantiate&amp;lt;/em&amp;gt;,
    &amp;lt;em&amp;gt;create&amp;lt;/em&amp;gt; objects from a class definition.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    This is the structure of the constructor. Look closely; it looks similar to a method, but it lacks a return type:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
    {ConstructorModifier} ConstructorDeclarator [Throws] ConstructorBody
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    But, it can be better understood with an example, so let&amp;#39;s see it in action in our &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    String name;
    int energy;

    Animal(String name) {   // &amp;amp;lt;-- Constructor added
        this.name = name;
        energy = 100;
    }

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Here we&amp;#39;ve created a constructor that accepts a &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; argument and assigns it to the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;
    field. It also initializes the &amp;lt;code&amp;gt;energy&amp;lt;/code&amp;gt; field with a value of &amp;lt;code&amp;gt;100&amp;lt;/code&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;blockquote class=&amp;quot;text-body-secondary&amp;quot;&amp;gt;
    Actually, there are other ways to create objects, but, to keep it simple, they&amp;#39;re not covered here. If you feel
    curious about it, you can read more about
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Class.html#forName(java.lang.String)&amp;quot;
       target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;Class.forName(String className)&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;,
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#clone()&amp;quot;
       target=&amp;quot;_blank&amp;quot; class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;Object.clone()&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;, and
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        &amp;lt;em&amp;gt;Deserialization&amp;lt;/em&amp;gt;
    &amp;lt;/a&amp;gt;.
&amp;lt;/blockquote&amp;gt;

&amp;lt;p&amp;gt;
    Up to here, we&amp;#39;ve defined the means to create an object of type &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;, but, how do we actually
    instantiate one?
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Simple, we use the
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;new&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    keyword.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
    Animal teo = new Animal(&amp;quot;Teo&amp;quot;);
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5 id=&amp;quot;nested-classes&amp;quot;&amp;gt;Nested classes&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;
    We&amp;#39;ve mentioned earlier that we can have
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        nested classes
    &amp;lt;/a&amp;gt;
    as a members of another class. But how does that actually look like? Well, it&amp;#39;s quite simple once we have understood
    fields and methods because their declaration follows the same principle: it&amp;#39;s a code structure that &amp;quot;belongs&amp;quot; to the
    class where they&amp;#39;re declared. More, formally: a &amp;lt;em&amp;gt;member class&amp;lt;/em&amp;gt; is a class whose declaration is
    &amp;lt;em&amp;gt;directly enclosed&amp;lt;/em&amp;gt; in the body of another class or interface declaration (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.5&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        JLS 8.5
    &amp;lt;/a&amp;gt;). Let&amp;#39;s see an example.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s suppose we want to represent each part of the body of an animal with a class. And then each animal would
    be formed by its parts &amp;amp;mdash;granted, this is a contrived example, only for demonstration purposes&amp;amp;mdash;
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt; At this point, we have two options:&amp;lt;/p&amp;gt;
&amp;lt;ol&amp;gt;
    &amp;lt;li&amp;gt;
        Create two independent classes, each in its own &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; file (&amp;lt;code&amp;gt;Animal.java&amp;lt;/code&amp;gt; and
        &amp;lt;code&amp;gt;BodyPart.java&amp;lt;/code&amp;gt;)
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
        Create one top level class (&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;), and one inner class (&amp;lt;code&amp;gt;BodyPart&amp;lt;/code&amp;gt;), both in one
        &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; file called &amp;lt;code&amp;gt;Animal.java&amp;lt;/code&amp;gt;
    &amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;
&amp;lt;p&amp;gt;
    How do we decide which option is better? Obviously &amp;quot;better&amp;quot; always depends on your specific use case, but for this
    example, let&amp;#39;s say a &amp;lt;code&amp;gt;BodyPart&amp;lt;/code&amp;gt; doesn&amp;#39;t make any sense without an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;, so it&amp;#39;s better
    to model the solution as if it&amp;#39;s a member of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. In this case we declare another class &amp;quot;inside&amp;quot;
    &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;, with all the same components we&amp;#39;ve discussed so far.
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    class BodyPart {      // &amp;amp;lt;-- inner class added
        String partName;
        double weight;
    }

    /**
     * Holds the information for each body part of this animal.
     */
    Collection&amp;amp;lt;BodyPart&amp;amp;gt; bodyParts;

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5 id=&amp;quot;nested-interfaces&amp;quot;&amp;gt;Nested interfaces&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;
    It&amp;#39;s difficult to explain what a &amp;lt;em&amp;gt;nested interface is&amp;lt;/em&amp;gt; without knowing what&amp;#39;s a Java
    &amp;lt;a href=&amp;quot;https://lealceldeiro.com/articles/2024/java-interfaces.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        interface
    &amp;lt;/a&amp;gt;.
    To keep it simple, and focus on classes, which is the topic of the article, I&amp;#39;ll oversimplify it: it&amp;#39;s basically
    a group of related methods with empty bodies, and its declaration is quite similar to that of a class. For example,
    this is an interface called &amp;lt;code&amp;gt;MovingBeing&amp;lt;/code&amp;gt;:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
interface MovingBeing {
    void move();
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Now, if we were to declare this interface as a member of the &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class, we just need to follow the
    same logic we used to declare the inner class &amp;lt;code&amp;gt;BodyPart&amp;lt;/code&amp;gt;:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    interface MovingBeing {     // &amp;amp;lt;-- inner interface added, not very useful now
        void move();
    }

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    More, formally: a &amp;lt;em&amp;gt;member interface&amp;lt;/em&amp;gt; is an interface whose declaration is &amp;lt;em&amp;gt;directly enclosed&amp;lt;/em&amp;gt; in the
    body of another class or interface declaration (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.5&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;
        JLS 8.5
    &amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;instance-and-static-initializer&amp;quot;&amp;gt;Instance and static initializer&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;
    There are two elements of the body of the class we haven&amp;#39;t covered yet: instance initializers (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-InstanceInitializer&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;InstanceInitializer&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    )
    and static initializers (
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-StaticInitializer&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;StaticInitializer&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    ).
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    They both are a
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-Block&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;block&amp;lt;/a&amp;gt;
    of code where some &amp;lt;em&amp;gt;initialization&amp;lt;/em&amp;gt; happens. The only difference between them is that the instance
    initializer is tied to the life-cycle of the object being created; hence, the code block is executed once for each
    object created. The static initializer, on the other hand, is executed only when the class is loaded (usually only
    once during the program execution). This is more related to the concept of
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html&amp;quot; target=&amp;quot;_blank&amp;quot;
       class=&amp;quot;link-underline link-underline-opacity-0&amp;quot;&amp;gt;
        &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;
    &amp;lt;/a&amp;gt;
    members (see
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.3.1.1&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;JLS&amp;lt;/a&amp;gt;).
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Let&amp;#39;s see them in an example.
&amp;lt;/p&amp;gt;

&amp;lt;h6&amp;gt;Instance initializers&amp;lt;/h6&amp;gt;

&amp;lt;p&amp;gt;
    Let&amp;#39;s suppose we want to move the initialization of the field &amp;lt;code&amp;gt;energy&amp;lt;/code&amp;gt; out of the constructor. We can do
    it by placing the expression &amp;lt;code&amp;gt;energy = 100;&amp;lt;/code&amp;gt; inside an instance initializer:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    String name;
    int energy;

    {
        energy = 100;       // &amp;amp;lt;-- initialization moved to this instance initializer
    }

    Animal(String name) {
        this.name = name;
    }

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h6&amp;gt;Static initializers&amp;lt;/h6&amp;gt;

&amp;lt;p&amp;gt;
    Now, let&amp;#39;s suppose that we want to have a field inside the &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class called
    &amp;lt;code&amp;gt;perceivedTemperature&amp;lt;/code&amp;gt;, which indicates the environment temperature perceived by the animals. In this
    case we don&amp;#39;t want to have a separate value for each animal, because we can say this temperature is the same
    for every animal &amp;amp;mdash;again, this is a contrived example, for demonstration purposes&amp;amp;mdash;
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    To store such field we should not do it with an instance field, but with a static one. This way, when the field is
    modified, we get the same value, regardless of the &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; instance the value is being accessed from. It
    would look like this:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    static double perceivedTemperature;

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    Now, we would like to initialize it in an instance initializer block, how do we do it? Simple:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    static double perceivedTemperature;

    static {
        perceivedTemperature = 32.4d;
    }

    // rest of the code omitted for brevity...
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h2&amp;gt;The &amp;lt;em&amp;gt;.java&amp;lt;/em&amp;gt; file&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;Where do we write our classes? Inside &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; files.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    Most of the time, each class is defined in its own &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; file. Such class is called a
    &amp;lt;em&amp;gt;top level&amp;lt;/em&amp;gt; class (type).
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    But it&amp;#39;s not mandatory to have only one top level type in a &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; file. We can define several of them,
    as long as &amp;lt;em&amp;gt;at most one&amp;lt;/em&amp;gt; of them is &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;. Put in another way: we can not declare more than
    one public top level type in the same &amp;lt;code&amp;gt;.java&amp;lt;/code&amp;gt; file.
&amp;lt;/p&amp;gt;

&amp;lt;h3&amp;gt;Putting it all together&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;Let&amp;#39;s see some examples where we apply all the knowledge we&amp;#39;ve learned.&amp;lt;/p&amp;gt;

&amp;lt;h4&amp;gt;Examples&amp;lt;/h4&amp;gt;

&amp;lt;h5&amp;gt;A public class with two fields, two methods, and one constructor&amp;lt;/h5&amp;gt;

&amp;lt;p&amp;gt;We saw this one already, but for the sake of completeness, let&amp;#39;s review it:&amp;lt;/p&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public class Animal {
    String name;
    int energy;

    Animal(String name) {
        this.name = name;
        energy = 100;
    }

    void move() {
        energy--;
        System.out.println(name + &amp;quot; moved&amp;quot;);
    }

    void eat() {
        energy++;
        System.out.println(name + &amp;quot; ate&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5&amp;gt;
    A public class with one protected static inner class, two fields, one instance initializer, and one
    static initializer
&amp;lt;/h5&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
public class Animal {
    protected static class BodyPart {
    }

    String name;
    static double perceivedTemperature;

    {
        name = &amp;quot;Leo&amp;quot;;
    }
    static {
        perceivedTemperature = 32.4d;
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5&amp;gt;A package-private class with one private static inner class, and one public static inner interface&amp;lt;/h5&amp;gt;
&amp;lt;blockquote&amp;gt;
    Side note: interfaces are &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; by default, but I&amp;#39;m putting it here for demonstration purposes only.
&amp;lt;/blockquote&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
class Animal {
    private static class BodyPart {
        String partName;
    }
    public static interface MovingBeing {
        void move();
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5&amp;gt;A sealed class that permits another final class to extend it&amp;lt;/h5&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed class Animal permits Tiger {
}
final class Tiger extends Animal {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h5&amp;gt;A sealed class that permits another non-sealed class to extend it&amp;lt;/h5&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code class=&amp;quot;language-java&amp;quot;&amp;gt;
sealed class Animal permits Tiger {
}
non-sealed class Tiger extends Animal {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;h2&amp;gt;Could we implement our programs entirely without classes?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    Certainly. Technically speaking, we don&amp;#39;t need to create our own classes to implement a business logic. But, by
    doing so, we can structure better our code and re-use it more effectively, we can achieve more robust and secure
    software and more adaptable to future changes. The enormous benefits of designing our code around OOP concepts
    such as classes and objects pay off immensely in the long term.
&amp;lt;/p&amp;gt;

&amp;lt;h2&amp;gt;Final thoughts&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;
    There are much more details to learn about Java classes and objects; we barely scratched the surface here, but
    hopefully, the content and links I&amp;#39;ve shared are a good start for you to continue learning on your own and
    apply that knowledge in building high quality software.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item><item>

      <title>The Java Development Kit (JDK)</title>
      <link>https://lealceldeiro.com/articles/2024/the-java-development-kit.html</link>
      <pubDate>Thu, 4 Jan 2024 00:00:00 +0000</pubDate>
      <guid isPermaLink="false">articles/2024/the-java-development-kit.html</guid>
      <description>
&amp;lt;p&amp;gt;
    The Java Development Kit (JDK) is a development environment for building applications and components using the
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/index.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;Java&amp;lt;/a&amp;gt;
    programming language. It includes tools useful for developing, testing, and monitoring such programs written in Java
    and running on the Java platform.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    The available versions of the JDK can be checked on the official
    &amp;lt;a href=&amp;quot;https://docs.oracle.com/en/java/javase/index.html&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;oracle website&amp;lt;/a&amp;gt;, where there&amp;#39;s
    detailed information about each specific release, along with instruction for their installation, and their
    &amp;lt;a href=&amp;quot;https://www.oracle.com/java/technologies/downloads/&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;download url&amp;lt;/a&amp;gt;. Speaking of
    instructions for installation, there are some nice tutorials in
    &amp;lt;a href=&amp;quot;https://dev.java/learn/getting-started/&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;dev.java&amp;lt;/a&amp;gt; that walk us through each specific
    step we must follow from the very beginning to get our first program up and running.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    When we download the JDK archive, we can find inside configuration files, &amp;lt;em&amp;gt;C&amp;lt;/em&amp;gt; header files, compiled Java
    modules, copyright and license files and additional libraries.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;key-commands&amp;quot;&amp;gt;Key commands&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;The JDK provides several commands to work with our Java programs. Some of them are:&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;javac&amp;quot;&amp;gt;&amp;lt;code&amp;gt;javac&amp;lt;/code&amp;gt;&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;This command converts .java source files into .class bytecode files.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Let&amp;#39;s suppose we have the following Java class inside a file called &amp;lt;em&amp;gt;MyClass.java&amp;lt;/em&amp;gt;:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code  class=&amp;quot;language-java&amp;quot;&amp;gt;
public class MyClass {
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    Now, if we run &amp;lt;code&amp;gt;javac MyClass.java&amp;lt;/code&amp;gt;, in the command line (at the same level the .java file is located at
    ), we&amp;#39;ll see that a file &amp;lt;em&amp;gt;MyClass.class&amp;lt;/em&amp;gt; is created.
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;java&amp;quot;&amp;gt;&amp;lt;code&amp;gt;java&amp;lt;/code&amp;gt;&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;This one is used to execute the Java program we write.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Following along with the previous example, let&amp;#39;s now add some code that prints &amp;quot;Hello world&amp;quot; in console and exits.
    First, let&amp;#39;s modify the class to look like this:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code  class=&amp;quot;language-java&amp;quot;&amp;gt;
public class MyClass {
    public static void main(String[] args) {
        System.out.println(&amp;quot;Hello world&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Now let&amp;#39;s re-compile it to get the new changes into the bytecode file (&amp;lt;code&amp;gt;javac MyClass.java&amp;lt;/code&amp;gt;).&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    Now we&amp;#39;re ready to run it. Let&amp;#39;s execute &amp;lt;code&amp;gt;java MyClass.class&amp;lt;/code&amp;gt; from the command line (notice we provided
    the compiled file as the argument &amp;amp;mdash;the .class file&amp;amp;mdash;). You should see &amp;quot;Hello world&amp;quot; printed in console.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    As a side note, starting with Java 11, Java applications can be run without going through the compilation step, as
    long as the program is written in a single file. So in the previous example, this would have worked as well:
    &amp;lt;code&amp;gt;java MyClass&amp;lt;/code&amp;gt;.
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;jar&amp;quot;&amp;gt;&amp;lt;code&amp;gt;jar&amp;lt;/code&amp;gt;&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;This one is used mainly to package files together.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    In our example, we could use it to create an archive called &amp;lt;em&amp;gt;MyProgram.jar&amp;lt;/em&amp;gt;, that contains one class file,
    &amp;lt;em&amp;gt;MyClass.class&amp;lt;/em&amp;gt;, and set &amp;lt;code&amp;gt;MyClass&amp;lt;/code&amp;gt; as the entry point. Like this:
&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code  class=&amp;quot;language-shell&amp;quot;&amp;gt;
    jar --create --file MyProgram.jar --main-class MyClass MyClass.class
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
    If you feel curious, you can run the program, as a jar file now, with &amp;lt;code&amp;gt;java -jar MyProgram.jar&amp;lt;/code&amp;gt; and you
    should see the exact same &amp;quot;Hello world&amp;quot; printed in console.
&amp;lt;/p&amp;gt;

&amp;lt;h5 id=&amp;quot;javadoc&amp;quot;&amp;gt;&amp;lt;code&amp;gt;javadoc&amp;lt;/code&amp;gt;&amp;lt;/h5&amp;gt;
&amp;lt;p&amp;gt;
    This command is used to generate the API documentation from Java source files as HTML pages.
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;If we add a documentation comment to our previous &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; method like this:&amp;lt;/p&amp;gt;
&amp;lt;pre&amp;gt;&amp;lt;code  class=&amp;quot;language-java&amp;quot;&amp;gt;
public class MyClass {
    /**
     * Entry point for our application.
     *
     * @param args Program arguments.
     */
    public static void main(String[] args) {
        System.out.println(&amp;quot;Hello world&amp;quot;);
    }
}
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
    And now run &amp;lt;code&amp;gt;javadoc MyClass.java&amp;lt;/code&amp;gt;, we can see all the HTML files created by the tool to include
    our documentation.
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
    All of these commands and more are documented in details in the official documentation website. You should
    definitely check it to get to know all the details about how to use them in you program.
&amp;lt;/p&amp;gt;

&amp;lt;h2 id=&amp;quot;what-about-the-jre&amp;quot;&amp;gt;What about the JRE?&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    JRE stands for Java Runtime Environment. It is a subset of the JDK that is not distributed anymore (
    &amp;lt;a href=&amp;quot;https://www.oracle.com/java/technologies/javase/11-relnote-issues.html&amp;quot;
       target=&amp;quot;_blank&amp;quot;&amp;gt;starting with Java11
    &amp;lt;/a&amp;gt;
    ). It only
    contained the tools needed to &amp;lt;em&amp;gt;run&amp;lt;/em&amp;gt; a Java application.
&amp;lt;/p&amp;gt;
</description>
      <author>asiel@lealceldeiro.com(Asiel Leal Celdeiro)</author>
      <source url="https://lealceldeiro.com"></source>
    </item>

  </channel> 
</rss>
