Is it possible to make a modification as specified in the class below, and initialize a member for existing callers to some default value, say null
?
Member is required to be private final as persistence requirement.
// initial version of the class
public class A {
A() {
// do some work here
}
}
// the following modification required adding additional constructor to the class with **member** data member.
public class A {
private final String member;
A(String member) {
this();
this.member = member;
}
A() {
// initilize member to null if a client called this constructor
// do some work here
}
}
Why can't you just have:
public class A {
private final String member;
A(String member) {
this.member = member;
}
A() {
this(null);
}
}
This is the usual pattern for constructor chaining; have the less-specific versions call the more-specific versions, supplying default parameters as appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With