5

If I have something like this:

public class SuperClass
{
    SuperClass()
    {
        x = true;
    }
    public boolean x;
}

public class SubClass extends SuperClass
{
    SubClass()
    {
        x = false;
    }
}

and I eventually make a SubClass object. Will x be true or false? From http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html#12.5 it looks like it will be false.

3
  • There's no connection between base and super classes in your example.
    – Maroun
    Dec 28, 2014 at 19:03
  • SuperClass and BaseClass are independent in your example.
    – August
    Dec 28, 2014 at 19:04
  • 5
    Your class names are confusing, shouldn't BaseClass be extending SuperClass? Also in that case shouldn't its name be more like SubClass or DerivedClass?
    – Pshemo
    Dec 28, 2014 at 19:04

2 Answers 2

10

From Section 12.5 of the Java Language Specification (relevant part in bold):

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

So the parent class's constructor will be invoked first (step 3), setting x to true. After the constructor of the superclass is processed and finished using the same steps recursively, the body of the child class's constructor will set it to false (step 5).

7

In the original version of the code [*], the two classes are unrelated and BaseClass won't even compile since there isn't an x declared or inherited.

If you made BaseClass a subclass of SuperClass, x will be false, since SuperClass's constructor will run before BaseClass's.

[*] Before somebody edited the question to add extend SuperClass.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.