• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Difference between constructors and setter methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75708
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 16960
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic