0

Contructors vs getters-setters

What is the advantage/disadvantage of using constructors over getters/setters? Are they interchangeable? Is one method preferred to the other?

22nd Jun 2017, 8:51 PM
Anirvan Komath Majumdar
Anirvan Komath Majumdar - avatar
3 Answers
+ 1
Values for the constructors are assigned when the class gets instantiated. But getters and setters are not
22nd Jun 2017, 8:59 PM
Yasiru Nayanajith
Yasiru Nayanajith - avatar
+ 1
Do not get a variable with a constructor. Constructors are generally used to initialize values upon object creation. Setters can set values at any time. Getters can get the variables/values at any time. You don't use setters and getters instead of constructors, you use constructors for initial values and getters or setters for later use.
22nd Jun 2017, 9:00 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
A constructor is used when the object is created. You can initialize you properties in the constructor. During the life time of a object, getters and setters are used to get information about the object and to set the properties of the object. You need to use both.
22nd Jun 2017, 9:01 PM
sneeze
sneeze - avatar