If I initialize a member variable outside of a constructor, when does the member actually get initialized? Is it guaranteed to be initialized for all possible constructors of the class?
public class MyClass
{
private String myName = "MyClass";
public MyClass(int constructor1Arg)
{}
public MyClass(int constructor2Arg1, int constructor2Arg2)
{}
}
Yes. All instance variable initializers are executed after superconstructor has executed, but before the body of any constructor declared in this class.
(As Jigar Joshi mentions, this is assuming the superconstructor executes normally.)
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