Immutable object on Java

Yup yup yup, after the CNY passed. Job seeking resume, as expected that it’s really hard to find a entry lv Java job especially when no any prior experience. But today is more about Java.

While starting the journey of organising previous code. Found out that I wrote a small segment of Java code which kind of explain how immutable object work under the context of Java. As always code is available on my Github and link attached as below.

Principle of a immutable object.

First of all, all field on the object need to be “final”. Which mean field not allow further edit after construction. So technically speaking, the only chance of modifying object state is on construction.

All attribute need to be final

screen-shot-2017-02-01-at-1-23-25-am

Constructer should be the only place to alter object’s state.

screen-shot-2017-02-01-at-1-25-09-am

So by following the principle, in most of the use case only getter should be deploy instead of setter.

screen-shot-2017-02-01-at-1-26-43-am

But how does it really come into kick on real-world application?  Dun got to know till get a Job to experience ne.  Keep tune of the Blog, reckon I would update more frequent as I am unemployed atm (Laugh).

https://github.com/andykhk/All-About-Java/blob/master/String_Joiner/src/Immutable_obj/ImmutableRGB.java

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s