<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Claudio Higashi</title>
    <description>The latest articles on DEV Community by Claudio Higashi (@claudiohigashi).</description>
    <link>https://dev.to/claudiohigashi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F188669%2Fe69438ac-58b8-472d-80dc-3500c884c40e.png</url>
      <title>DEV Community: Claudio Higashi</title>
      <link>https://dev.to/claudiohigashi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/claudiohigashi"/>
    <language>en</language>
    <item>
      <title>Mocking Logger in Java with Mockito</title>
      <dc:creator>Claudio Higashi</dc:creator>
      <pubDate>Tue, 31 Mar 2020 21:40:42 +0000</pubDate>
      <link>https://dev.to/claudiohigashi/mocking-logger-in-java-with-mockito-51k8</link>
      <guid>https://dev.to/claudiohigashi/mocking-logger-in-java-with-mockito-51k8</guid>
      <description>&lt;p&gt;Yes, you read it correctly: "&lt;em&gt;Mocking Logger in Java&lt;/em&gt;". That might sound very weird when you see this for the first time but there are some particular cases in which the only way to unit test some parts of your application is by checking whether some specific messages were logged or not.&lt;/p&gt;

&lt;p&gt;For example, suppose that your application needs to make an asynchronous call to some other WebService. Then you have separate threads for making that call. For the main thread, it doesn't matter if the call to the service was successful or not because it won't wait for a response (otherwise it would be synchronous, right?). Suppose that this part of your application will just call the service and, after that, only log whether the call was successful or not (needless to say that in such a case, you need a monitoring tool in place to know when things are not going well in production).&lt;/p&gt;

&lt;p&gt;Now, you have to create a JUnit test case to cover these scenarios. If you google it, you are going to find some people using &lt;em&gt;PowerMock&lt;/em&gt; for this, but imagine that you are already using &lt;em&gt;Mockito&lt;/em&gt; and, for some reason, you want to do this using &lt;em&gt;Mockito&lt;/em&gt; only.&lt;/p&gt;

&lt;p&gt;In this post I'm assuming that you are already familiar with &lt;em&gt;Mockito&lt;/em&gt;, so I'll focus on the "&lt;em&gt;mocking logger&lt;/em&gt;" part only. You can find lots of &lt;em&gt;Mockito&lt;/em&gt; tutorials on the Internet if you want to learn it.&lt;/p&gt;

&lt;p&gt;Okay. One way to accomplish this is to mock the &lt;em&gt;Appender&lt;/em&gt; of your &lt;em&gt;Logger&lt;/em&gt; object and use &lt;em&gt;Mockito&lt;/em&gt;'s &lt;em&gt;ArgumentCaptor&lt;/em&gt; to capture all logging events in your test case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Mock&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Appender&lt;/span&gt; &lt;span class="n"&gt;mockedAppender&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Captor&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ArgumentCaptor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;LoggingEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loggingEventCaptor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you have to make sure that the mocked &lt;em&gt;Appender&lt;/em&gt; is added to root &lt;em&gt;Logger&lt;/em&gt; before the tests are executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Before&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;LoggerFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ROOT_LOGGER_NAME&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addAppender&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockedAppender&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setLevel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Level&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INFO&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this you are ready to call the tested class and do the assertions on the logged messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testSuccessCall&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// mock all required response objects in here&lt;/span&gt;

        &lt;span class="o"&gt;[...]&lt;/span&gt;

        &lt;span class="c1"&gt;// this is the call to the service being tested&lt;/span&gt;
        &lt;span class="n"&gt;myService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;createRequestForSuccessResponse&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="c1"&gt;// check how many times the ArgumentCaptor was called&lt;/span&gt;
        &lt;span class="n"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockAppender&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;doAppend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loggingEventCaptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;capture&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="c1"&gt;// get the reference to the LoggingEvent you want to inspect&lt;/span&gt;
        &lt;span class="nc"&gt;LoggingEvent&lt;/span&gt; &lt;span class="n"&gt;loggingEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loggingEventCaptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAllValues&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// check the logged message&lt;/span&gt;
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Webservice was successfully called"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loggingEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="c1"&gt;// check the log level&lt;/span&gt;
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Level&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INFO&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loggingEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLevel&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this might be eventually helpful when you face such a particular scenario! ;-)&lt;/p&gt;

</description>
      <category>java</category>
      <category>testing</category>
    </item>
    <item>
      <title>Agile for Kids, literally</title>
      <dc:creator>Claudio Higashi</dc:creator>
      <pubDate>Mon, 30 Mar 2020 16:01:05 +0000</pubDate>
      <link>https://dev.to/claudiohigashi/agile-for-kids-cpk</link>
      <guid>https://dev.to/claudiohigashi/agile-for-kids-cpk</guid>
      <description>&lt;p&gt;The word &lt;em&gt;Kids&lt;/em&gt; in the title is literal: &lt;strong&gt;kids&lt;/strong&gt;! I have kids at home and nowadays they are required to study from home due to the current circumstances (the pandemic, obviously), the same way as I am also forced to work from home. Initially, they were kind of very happy about having not to go to school, but after the first week, I noticed that things started to change. Things were getting out of their control. They started to become desperate for not knowing exactly what to do. Lots of homework, online tests, and other activities. Now, their responsibility is way bigger than before, when they were present at school and teachers usually say to you what to do and when.&lt;/p&gt;

&lt;p&gt;That's where I came in. So I was aware that I had to wear the hat of "&lt;em&gt;Scrum Master&lt;/em&gt;" at home. So I decided to apply some agile methodologies which we use in the company to facilitate their lives, to give them focus on what's really important. I bought one whiteboard per daughter and colored paper stickers. Each color represents a type of activity: yellow for tests, pink for homework, and blue for other activities, for example.&lt;/p&gt;

&lt;p&gt;Then we created a board with all stories which we identified together in a sort of light planning session. We identified the important stories for the week and glued all of them to the TODO session.&lt;/p&gt;

&lt;p&gt;These were the sessions of their Kanban boards:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Backlog: all remaining stories&lt;/li&gt;
&lt;li&gt;To do: starts with all the important stories for the week&lt;/li&gt;
&lt;li&gt;Doing: the story which she is working on (should be only one)&lt;/li&gt;
&lt;li&gt;Done: all completed stories&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1bf-rek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3p5g2efn3k45a22yh2q9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1bf-rek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3p5g2efn3k45a22yh2q9.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now they know exactly what to do and they are way more confident knowing that everything is there on the board and under control.&lt;/p&gt;

</description>
      <category>agile</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Locating jar files containing a given class in the command line</title>
      <dc:creator>Claudio Higashi</dc:creator>
      <pubDate>Thu, 26 Mar 2020 22:12:28 +0000</pubDate>
      <link>https://dev.to/claudiohigashi/locating-jar-files-with-a-given-class-in-the-command-line-4k82</link>
      <guid>https://dev.to/claudiohigashi/locating-jar-files-with-a-given-class-in-the-command-line-4k82</guid>
      <description>&lt;p&gt;Have you ever been in a situation where you need to check whether a given Java class is present or not in some jar library in a remote server's filesystem but all you have in your hands is just a *%@!# SSH terminal? You just feel like you have to cross the ocean with a spoon.&lt;/p&gt;

&lt;p&gt;In such a case, and if luckily the remote server is running some sort of Linux, then you can use the &lt;em&gt;find&lt;/em&gt; command in combination with &lt;em&gt;grep&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The example below will try to locate which jar files contain the class &lt;em&gt;com.ibm.db2.jcc.Db2Driver&lt;/em&gt;, starting from the current directory. Notice that we have to replace &lt;em&gt;dots&lt;/em&gt; by &lt;em&gt;slashes&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.jar'&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-Hs&lt;/span&gt; com/ibm/db2/jcc/DB2Driver &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what I got after running the above command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Binary file ./WEB-INF/lib/db2jcc4-10.5.2.jar matches
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this may eventually help you to cross your ocean. But take care. Sometimes there are lots of sharks out there! :-o&lt;/p&gt;

</description>
      <category>java</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>SSH Tunneling via a Jump Host</title>
      <dc:creator>Claudio Higashi</dc:creator>
      <pubDate>Wed, 25 Mar 2020 21:50:12 +0000</pubDate>
      <link>https://dev.to/claudiohigashi/ssh-tunneling-via-a-jump-host-2b5d</link>
      <guid>https://dev.to/claudiohigashi/ssh-tunneling-via-a-jump-host-2b5d</guid>
      <description>&lt;p&gt;Many large companies use to implement strict rules for accessing servers hosting their applications. One of the security measures some of them implement is to place a gateway between "you" and the server you need to access. This gateway is also known as Jump server, Jump host, or Jump box.&lt;/p&gt;

&lt;p&gt;A typical implementation of this requires you to, firstly, open an SSH connection to the Jump host with your own credential and, secondly, from the inside of the Jump host, open a second SSH connection to the actual server you need to access with another account (usually a non-personal account which can be the user used to deploy and run your application).&lt;/p&gt;

&lt;p&gt;Let's suppose that you work in a company like this and that you want to create an SSH tunnel to port 1521 of an Oracle Database Server which is only accessible from your application server. What you need to do is to create an SSH tunnel like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgshare.io%2Fimages%2F2020%2F03%2F25%2Fssh-tunneling-via-jump-host-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgshare.io%2Fimages%2F2020%2F03%2F25%2Fssh-tunneling-via-jump-host-2.png" alt="SSH Tunneling via Jump Host"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following command creates this SSH tunnel via the Jump host (you will be prompted for the users' password):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nt"&gt;-N&lt;/span&gt; appusr@appserver &lt;span class="nt"&gt;-J&lt;/span&gt; myusr@jumphost &lt;span class="nt"&gt;-L&lt;/span&gt; 1521:dbserver:1521


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With this command, you are tunneling the port &lt;em&gt;1521&lt;/em&gt; of &lt;em&gt;localhost&lt;/em&gt; to the port &lt;em&gt;1521&lt;/em&gt; of &lt;em&gt;dbserver&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In other words, you are firstly doing an SSH connection to &lt;em&gt;jumphost&lt;/em&gt; with user &lt;em&gt;myusr&lt;/em&gt;, then another SSH connection to &lt;em&gt;appserver&lt;/em&gt; with user &lt;em&gt;appusr&lt;/em&gt;, and finally forwarding port &lt;em&gt;1521&lt;/em&gt; from localhost to &lt;em&gt;dbserver&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This command is being executed in verbose mode (&lt;em&gt;-v&lt;/em&gt;), which is useful for debugging, and not returning the shell prompt of the &lt;em&gt;appserver&lt;/em&gt; but just forwarding the port (&lt;em&gt;-N&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;After this, you can then use your preferred SQL client tool to connect to the remote database server as if it was running on &lt;em&gt;localhost:1521&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Changing a constant in Java at runtime (WHAT?!)</title>
      <dc:creator>Claudio Higashi</dc:creator>
      <pubDate>Tue, 24 Mar 2020 20:08:55 +0000</pubDate>
      <link>https://dev.to/claudiohigashi/changing-a-private-constant-in-java-what-33m9</link>
      <guid>https://dev.to/claudiohigashi/changing-a-private-constant-in-java-what-33m9</guid>
      <description>&lt;p&gt;When learning Java, you were told that constants can be created by using the &lt;em&gt;final&lt;/em&gt; keyword and that the value (or the object reference) they hold can never be changed, otherwise, they wouldn't be known as constants, right?&lt;/p&gt;

&lt;p&gt;That's almost true. In fact, you can change the value of a constant with the use of a technique called Reflection. With reflection, you are able to programmatically access and change information about fields, methods, and constructors of loaded classes at runtime.&lt;/p&gt;

&lt;p&gt;In this example, we are changing the private constant AGE from 50 to 15:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.reflect.Field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.reflect.Modifier&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ModifyConstant&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;OldPerson&lt;/span&gt; &lt;span class="n"&gt;fakeYoungPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OldPerson&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;changePrivateConstant&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fakeYoungPerson&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"AGE"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;fakeYoungPerson&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sayYourAge&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;changePrivateConstant&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;constantName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// gets the Field object which represents the constant&lt;/span&gt;
        &lt;span class="nc"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getDeclaredField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;constantName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// need this because AGE is 'private'&lt;/span&gt;
        &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAccessible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;modifiers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getDeclaredField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"modifiers"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// need this because modifiers of any Field are 'private'&lt;/span&gt;
        &lt;span class="n"&gt;modifiers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAccessible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// this kind of removes the 'final' keyword.&lt;/span&gt;
        &lt;span class="c1"&gt;// it actually turns off the bit representing the 'final' modifier&lt;/span&gt;
        &lt;span class="n"&gt;modifiers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getModifiers&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FINAL&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// and now we are able to set the new value to the constant&lt;/span&gt;
        &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;set&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OldPerson&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="no"&gt;AGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sayYourAge&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;AGE&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" yo only!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting output after compiling and running this class is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'm 15 yo only!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course that changing a constant is a completely weird use case, although I think that might have caught your attention to this (right?). Nevertheless, reflection is a powerful technique that helps developers to create dynamically configurable components. You also need to be aware that reflection costs a lot in terms of performance, so use it wisely and only when really needed.&lt;/p&gt;

&lt;p&gt;The last thing that I would like to mention, is that this particular example wouldn't work if the constant is of type &lt;em&gt;String&lt;/em&gt; because of the &lt;em&gt;Java String Constant Pool&lt;/em&gt;, but this is a different story which I will leave for later...&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
