Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setValue vs Constructor  RSS feed

 
Isaac Ferguson
Ranch Hand
Posts: 1063
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a question.

What are the advantages vs disadvantages of this two approaches

Using an existing Integer for set a value

Integer integer=new Integer();

integer.setValue(1);

Or set the value using a constructor?

Thanks




 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angus Ferguson wrote:What are the advantages vs disadvantages of this two approaches

Using an existing Integer for set a value

Integer integer=new Integer();

integer.setValue(1);

The main disadvantage of using this approach is that it won't compile because the Integer class does not have a no-arg constructor nor a setValue method.

If you are talking more generally, then you should only use a no-arg constructor if there are valid default values for all fields in your class. If your class has fields that have no valid default value, then you should make sure that the constructors always supply valid values for these fields.
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that null might be a valid default value as well. You should minimize that to a minimum though.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many a times a mater of style which one you prefer.

Pro & Cons of Constructor style
1) Constructors with too many Arguments become difficult to use, even providing multiple overload constructors might be confusing.
2) Class could be designed for immutability. With setters immutability is more difficult.
3) Good for setting mandatory parameters

Settlers
1) More Flexible
2) Good for optional parameters
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd ask myself two questions.

1. Do I need the object to be mutable? That is, do I need to be able to change the field value after the object is created? Bear in mind there are advantages to immutability.

2. Do I want it to be possible to create an instance of the class without providing initial values to the fields?

The answer to those questions tells you whether you should provide setters and whether you should set fields with a constructor.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is mutable, so you want to change its state via plain mutator methods or some other way?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!