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.

Recommended Answers

All 3 Replies

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);

etc

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.