Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Is member initialization outside of constructor always guaranteed to be invoked?

Tags:

java

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)
    {}
}
like image 251
zer0stimulus Avatar asked Nov 30 '22 06:11

zer0stimulus


1 Answers

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.)

like image 144
Jon Skeet Avatar answered Dec 18 '22 07:12

Jon Skeet