immutable vs mutable objects

Posted on 2012-09-17
Last Modified: 2012-09-20
I was reading as below but I have not clearly understood how we can set wothout setter methods and concept behind immutable vs mutable objects.Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance

There's no need to have setting methods on UserDetail.  It's designed to be "set" through the constructor:

UserDetail oldDetail = new UserDetail("my-name",'mypassword") ;

so to change it you do something like:
UserDetail newDetail = new UserDetail(oldDetail.getUsername(), "my-new-password") ;

 The UserDetail class and setter methods not being required.

 "immutable vs mutable objects" and that will give you more info on why the UserDetail class is the way it is.

Basically though, it you don't NEED setter methods, don't put them in.
In this case you are only ever setting the username/password once, so your use case is a good fit for an immutable object. Also, if your needs ever do change, it is easy to add the setter methods at that point.
Question by:gudii9
    LVL 16

    Expert Comment

    You "set without setter methods" by only allowing data to be set during construction. So technically it is "set once".
    If you do need to change it, you can do so by creating a new object instead.

    This is the way that the String class works. It is a final, immutable, class. You can create a new string. But once created, it cannot change. You can, instead, create a different String and use that.
    LVL 16

    Accepted Solution

    There are two main advantages to having an immutable class. The first, more minor one, is that no code is needed to deal with changing data. In the String class, for instance, there is no need for code to allocate extra space if the String gets larger.
    The second advantage is with respect to multithreading. The String class, for example, can be treated like a primitive. There is no need to worry about the String data being in the middle of a change when context is switched to a different thread which would leave the String class in an indeterminant state. If you needed to do that you would have to introduce some synchronization to ensure that it doesn't happen, which has certain performance costs.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now