Hello, i have a simple question.

Let's say you have 3 private attributes.
If you want to insert a value you use a set method and when you wish to display its content you use a get method.

When you use a constructor with a parameter list e.g.:

private String attribute1;
private int attribute2;
private double attribute3;
Constructor(String attr1, int attr2, double attr3) {

attribute1 = attr1;
attribute2 = attr2;
attribute3 = attr3;

In the above case, the insertion of the values is made via the constructor and not the set method. So, is it false to add a value in the private attributes via the constructor and not via the set method?

Thank you for your time.

6 Years
Discussion Span
Last Post by javitis

Ideally, your constructor should call the setter methods instead of directly assigning the values to the private members. So, it would do something like...

attribute1 = setAttribtue1(attr1);



It's often done that way, but ideally the constructor should also use the set() methods to store the parameter values, thus ensuring that any side effects are properly handled.

Edited by JamesCherrill: Posted in parallel with previous reply


Oh, right! That's more OOP. Thank you again very much!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.