• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor or Getter/Setter Mehods  RSS feed

 
Andy Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is better for creating objects, Constructors or Getter/Setter methods? Also, would both of these be considered two ways to achieve the same results when creating objects?

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only a constructor can be used to create an object. That's what a constructor is for. Other methods, whether you would call them getters or setters or something else, can only be applied to objects which already exist.

Now besides that, perhaps you are wondering whether a constructor or a setter method is preferable for assigning a value to an attribute of an object. Well, that depends on the design of the class. If you want the object to be immutable (at least with respect to that attribute) then you want the attribute to be set exactly once. The way to achieve that is to pass the attribute's value as a parameter of the constructor, and that works because every time you call the constructor it constructs another object of the class. If you provide a setter method which assigns a value to the attribute, then it can be called more than once on an object, which means that the object is not immutable (with respect to that attribute).

There are situations where you would want one or the other, so you can't just classify either option as "better". If you need a hammer, then use a hammer. If you need a screwdriver, then use a screwdriver. But you can't say that either the hammer or the screwdriver is "better" no matter what your needs are.
 
Andy Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's Paul ... it all makes sense now.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the real reason we get asked that sort of question is because people come across the Bean Pattern and don't understand its use.
Standard encapsulation: Use the constructor, which sets all fields to sensible values (officially called establishing the class invariants). No need for any setXXX methods, but other methods may change the state of the object. For example a BankAccount object will have depositMoney and withdrawMoney methods.
Bean Pattern: You are working in an environment where everybody is skilled and trustworthy. They know not to violate your class invariants. Your classes all have no‑argument constructors and both setXXX and getXXX methods for every field. You can use code from the bean package to write the state of your object into an XML file and later reconstruct the object from that XML. If you look carefully at the constructors for a few classes, e.g. this constructor, you will see an annotation allowing you to use a constructor with arguments in a variant of the Bean Pattern. There are some widely‑used development tools which require objects conform to the Bean Pattern. You therefore need to know about the Bean Pattern, even though you might not use it at this stage of your career.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!