Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

immutable vs mutable objects

Posted on 2012-09-17
2
Medium Priority
?
464 Views
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.
0
Comment
Question by:gudii9
  • 2
2 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 38406106
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.
0
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 38406127
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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

577 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