When is Java Java implicit constructor called compared to the base class?

If I have something like this:

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

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

+5
source share
2 answers

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

Before the result is a reference to the newly created object, the specified constructor is processed to initialize the new object using the following procedure:

  • Assign constructor arguments to the newly created parameter variables for this constructor call.

  • If this constructor begins by explicitly calling the constructor (ยง8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process the constructor call recursively using these five steps. If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason; otherwise go to step 5.

  • This constructor does not start by explicitly calling the constructor of another constructor in the same class (using this). If this constructor belongs to a class other than Object, then this constructor will start by explicitly or implicitly calling the constructor of the superclass (using super). Evaluate the arguments and handle the call to the superclass constructor recursively using these five steps. If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason. Otherwise, go to step 4.

  • Run instance initializers and instance variable initializers for this class, assigning the initializer values โ€‹โ€‹of the instance variable to the corresponding instance variables in the order from left to right, in which they are displayed in text form in the source code for the class. If the execution of any of these initializers results in an exception, then no new initializers are processed, and this procedure terminates abruptly with the same exception. Otherwise, go to step 5.

  • Run the rest of the body of this constructor. If this execution completes abruptly, then this procedure terminates abruptly for the same reason. Otherwise, this procedure is performed normally.

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

+10
source

In the original version of the code [*], the two classes are not related to each other, and BaseClass is not even compiled because x not declared or inherited.

If you made BaseClass subclass of SuperClass , x will be false since the SuperClass constructor will work before BaseClass .

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

+7
source

Source: https://habr.com/ru/post/1210005/


All Articles