• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between constructors and setter methods  RSS feed

 
Vignesh Jeffry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very much confused about the setter method and the constructor in a class. My question is when you can set value using setter method then why use constructor to set value when both does the same function.

Ex:



[Added code tags - see UseCodeTags for details]
 
Rajeev Motha
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may use the constructor to set values at point of instantiation. However, an object may have many attributes that 1) you may not know at the point of instantiating or 2) may need to be changed after instantiating.
For this you need setters.
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vignesh Jeffry ,

Welcome to JavaRanch!

-------------------------
Regarding Constructing Objects
-------------------------

You are right. They do similar things. It is up to you. However, there may be times when you will prefer one over the other.

Example. I like making things short. I prefer



instead of



When you are coding thousands of lines, and lots of logic, you prefer shorter quick codes than longer unnecessary lengthy codes.

Most often though, I dont code the constructors until I need to use them.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Calling the setters in the code creation is different to having setters in the class or not,

For having cleaner and smaller code its better not to have setters in the class and set the value directly in the constructor and only introduce the setter if there is a need for it, if you dont need to have a setter than why have more code than what is needed :)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing about constructors are:
- They have to be called when creating an object
- They are only called once per object.

So you should definitely use a constructor if:
- A value has to be provided. You can't force someone to call a setter, but with a constructor you can force someone to pass a suitable value in.
- A value should be provided once and then never changed. For example, if you're writing an immutable class. In this case you'll have a constructor but no setter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vignesh Jeffry wrote:My question is when you can set value using setter method then why use constructor to set value when both does the same function.

You might do better to turn the question around and ask: if you can set a value in a constructor, why do you need a setter?
And the answer is: to change a value - and the need for that is not as great as you might think.

Providing public setters makes a class mutable, and that's not always a good thing; so my advice would be to resist the temptation to provide setter methods unless you're absolutely sure you need them.

Winston
 
Vignesh Jeffry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. By the way i came to know that the prime difference is setter methods can handle user exceptions. Is it that true?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vignesh Jeffry wrote:Thanks all. By the way i came to know that the prime difference is setter methods can handle user exceptions. Is it that true?

It's certainly not the prime difference. Whether it's a difference at all depends on what you mean by that. But the prime difference is that a setter allows the value to be changed after it was initialised.
 
Samuel Mayol
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev Motha wrote:You may use the constructor to set values at point of instantiation. However, an object may have many attributes that 1) you may not know at the point of instantiating or 2) may need to be changed after instantiating.
For this you need setters.


I am going to provide you a link with some information that I provide about this topic:

Setter, Getter and constructor in Java: http://stackoverflow.com/questions/19359548/setter-methods-or-constructors/42798426#42798426

Just click the link and you will prompted to the explanation and examples. Thank you.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I don't think you got very good answers on the SO th‍read, I am afraid. The amount of explanation there was, I am afraid, minimal.
 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I don't think you got very good answers on the SO th‍read, I am afraid.

Especially that one reply:
S. Mayol on SO wrote:Here is a formula that is going to show you what I mean:

Private fields + Public accessors == Encapsulation;

You could argue that public accessors often break encapsulation rather than preserve it, especially when they return object references.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!