Reputation: 9194
If I have a customer class with an overloaded constructor (default and one with params) what is the proper way to set the class members in the Overloaded constructor? Using "this" references or using the setter methods?
Just wasn't sure what the proper method was.
public class Customer {
private String firstName;
private String lastName;
private int age;
public Customer() {}
//This Way
public Customer(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
// Or this way?
public Customer(String firstName, String lastName, int age)
{
setFirstName(firstName);
setLastName(lastName);
setAge(age);
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
Upvotes: 21
Views: 2107
Reputation: 37598
As was already stated it's quite hazardous to call overridable methods in constructor. However if you need to perform some sort of validation that takes place in the setter you still have a few sensible ways to achieve it.
Upvotes: 0
Reputation: 33534
Its Not about which is a better way to do it, but what you want out of it......
- If you want your class to be mutable
, then go with setters
.
- If you want your class to be Immutable
then i think using this
, is a better option.
I think using this
, is apt in places, where you receive some data from a webserver or some source, then store them into an Collection in form of Instances of a Custom class.....
Eg:
Create a Class Student,
As you make a request to some web-service, you will get the response, for eg: JSON
...
Parse it, then Create an Instance of Student and Store it in an Collection..
eg:
ArrayList<Student> arList = new ArrayList<Student>();
arList.add(new Student(name,rollNos,class,marks));
Upvotes: 2
Reputation: 860
The best answer would be "depends". Generally, you don't need to use setters unless setters do something more like a calculation before setting the value. However, if your setters only set the value directly, then this
is probably best for you. On the contrary, setters are used for validation and stuff, if you use this
, you'll miss out on them.
Upvotes: 1
Reputation: 3199
The direct access way is faster (7 times faster if memory serves me correctly) but the second one is "safer" in case some special rules to assigning those values or chaining of assignment (where you assign a second field from the setter of another) where implemented in the accessors, that is unless of course you want to explicity bypass those rules/chainings when the assignment is done from the constructor.
So in short, it depends on your needs but those are the main concerns I can think of.
Upvotes: 0
Reputation: 18424
The first one (using this.
) is probably safer and more straightforward. Consider if a future subclass overrode the setter methods - this could cause very unexpected behavior.
If your class is final, this is irrelevant and it's a wash.
Upvotes: 27
Reputation: 24865
Basically, there is no difference except for validation.
On one hand, if your setter validates new values based only in the new values (they do not depend of the object state), calling the setter avoids duplicating the validation logic.
On the other hand, if the validation of one setter checks other properties, then you either must be sure that the needed properties are already set before calling that setter, or assign the property directly.
Upvotes: 0