For Starter, you can use the constructor
to set the value of your attribute at the point of instantiation your object but many case It will be problem for you because Object
may have different value according to context they are put into.
After Constructor
is used and Your Object
is instantiated (Constructor are only once per Object) what if you need to change the value of an attribute. You can create new Object
but it is waste of resources. If Dependency is not an option in your code then only go for constructor.
To meet the requirement of 4 principle of OOP
, Getter
and Setter
method is the key to achieve it.
It depend on you design pattern too, for Immutable Object Creation
, you have to use constructor because you cannot change state of the Object after it's creation.
Getter
and Setter
is the reason why Pojo Object are consider more powerful, when you have to code 100 of LOC which have numerous number of logic in it. But with the constructor you have to create constructor of different parameter according requirement that fulfill your logic that lead to number of dependencies toward class.
For future purpose, Constructor
should only used for instantiating the object Like Single responsibility principle
in SOLID
.