Platinum Solutions Corporate Website


The answer you entered to the math problem is incorrect.
W. Craig Trader (not verified) Wed, 1969-12-31 20:00

The behavior you've demonstrated is exactly what you should expect Java to do in this instance, and it's documented in the Java Language Specification, section 12.5.

The short form is that when creating an instance of an object, the constructor will make any implicit or explicit calls to super class constructors first, then do any class-specific initialization, and then finally do any specific statements in the constructor.

In your example, "private String varA = null;" is actually interpreted by the compiler as two separate statements: (1) "private String varA;" which is scoped to the class, and (2) "varA = null;" which is scoped to *each* constructor for the class.

As a side note, all of byte code for all of the default initializations will be included in each constructor for a given class.  This becomes important to understand when you start measuring code coverage of unit tests where you're testing classes with multiple constructors.

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2.
The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options