-1

I see we can encapsulate instance variables with setters, however, Constructors seem to do the same thing.

Class object1 = new Class(100, 100) // setting using Constructor

object1.setValue(100, 100)  // setting using Setters

When should I use setters, and when should I use Constructors to initialize instance variable values?

Togusa
  • 9
  • 1
  • 1
  • 4
  • There is a similar question here: http://stackoverflow.com/questions/7779509/setter-di-vs-constructor-di-in-spring – David Pérez Cabrera Sep 09 '16 at 21:37
  • 2
    It's a matter of opinion; here's mine: use constructors when possible to reduce code, but I wouldn't recommend setting multiple values through a setter. – ChiefTwoPencils Sep 09 '16 at 21:37
  • One possible time to prefer one over the other: [BeanShell](http://www.beanshell.org/) or any other framework that expects a [bean](https://en.wikipedia.org/wiki/JavaBeans). – Elliott Frisch Sep 09 '16 at 21:39
  • There is very obvious musts: only a constructor can initialize a consistent state and can also take care of `final` members, only setters can update the state during the lifetime of the object. – Thomas B Preusser Sep 09 '16 at 21:40
  • 1
    This question might be too broad, but I don't think it's primarily opinion-based. There are specific memory-model implications, for example, that support immutability. – chrylis -cautiouslyoptimistic- Sep 09 '16 at 21:44
  • @chrylis, fair enough. Seemed to be a classic "which is better" question but I see your point. – ChiefTwoPencils Sep 09 '16 at 21:55

3 Answers3

3

Use the constructor when the value is required for the object to make sense, or when you need to provide multiple values at once for consistency or convenience; for example, a Rectangle must have a length and a width.

Rectangle r = new Rectangle(10, 20);

Provide setters when you have optional values (such as a color) or values that can be changed after the object is created:

r.setColor(Color.BLUE);

Note that it's usually preferable to create immutable "value objects" whenever possible, since these eliminate several types of potential bugs. Value objects must be initialized all at once, though they frequently also have methods that return a new, similar object:

r = r.withLength(15);
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

Constructors are used when you are creating the first instance of an object. Setters are used when you are changing a property of that object.

Jack Miller
  • 325
  • 1
  • 16
0

Let me relate it with real life scenerio.

I am developer working for XXX organization and uses YYY brand computer.

In Java this corresponds to 4 classes, class YYY, class XXX, class Developer, class Computer.

  • Since YYY company manufactures the computer - so class YYY must have constructor for class Computer.
  • Since organization XXX configures the computer for me to use, so class XXX must have setter for class Computer (not the constructor). eg setOS(), setJava()
  • Since i use that computer, class Developer will have on getter methods, neither setter nor constructor.

I hope this is going to help you.

Sachin Goyal
  • 98
  • 2
  • 12