Issue
For example: Which is better practice? If there is any difference at all.
class IceCream {
String flavour = "vanilla";
public IceCream() {
}
}
or
class IceCream {
String flavour;
public IceCream() {
flavour = "vanilla";
}
}
Solution
Technically no difference - you just need to remember that field initialization is happening before calling constructor(s) and processing init blocks
Privately I prefer the field approach because you do not need to add boiler plate code and see the initial value next to the declaration just after entering the class.
Of course remember that not always you are able to use inline initialization
Answered By - m.antkowicz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.