In Java, but also in other OO languages, is there a difference between initializing an attribute in its definition, as in
class Example {
public Sample sample_attribute = new Sample();
}
and using the constructor to initialize it?
class Example {
public Sample sample_attribute;
public Example() {
sample_attribute = new Sample();
}
}
I could not come up with any practical difference, is there any? Otherwise, there are cases when one method is better than another, even if they have the same result?
source
share