Order of class initialization/instantiation

Asked

Viewed 797 times

2

class Bird {
 { System.out.print("b1 "); }
 public Bird() { System.out.print("b2 "); }
}
class Raptor extends Bird {
 static { System.out.print("r1 "); }
 public Raptor() { System.out.print("r2 "); }
 { System.out.print("r3 "); }
 static { System.out.print("r4 "); }
}
class Hawk extends Raptor {
 public static void main(String[] args) {
 System.out.print("pre ");
 new Hawk();
 System.out.println("hawk ");
 }
}

The result displayed in the above code execution screen is:

r1 r4 pre b1 b2 r3 r2 hawk

However, I doubt that. I understand how the Hawk class needs the Raptor class, but the Raptor class needs the Bird class, so I thought B1 should be the first to be shown. It would be possible to explain step by step how the initializations happen and justify why?

  • 1

    First are executed the static members(so R1, R4 and pre were the first), then follow the order of inheritance, as you well noted.

  • 2

    Related http://answall.com/q/104512/101

1 answer

5


In java, the boot order is as follows:

1-Executes all static components in the order in which they appear as soon as the class is loaded into the JVM

2-Executes all instance components in the order in which they appear in the class, so an object of this class is instantiated

3-Executes the constructor.

Therefore, first members will be executed with the word reserved static, in the case of the class informed, were the calls static { System.out.print("r1 "); }, static { System.out.print("r4 "); } and the very main, which is also static.

Then, the instance members of the classes were executed, in the order in which they appear, in the case of the code presented, only the call { System.out.print("b1 "); }.

Now it is beginning to call the builders, however, there is inheritance between the classes, which makes the one that is "higher" (in this case the Class Bird) be the first to have its constructor, for the first thing a subclass does is to call the constructor of its superclass, then first the constructor of Bird, after Raptor, for finally the class builder Hawk run, and only after that call System.out.println("hawk "); was exhibited.

To better understand, I made this example:

public class TesteClasse {

    public static void main(String[] args) {
        new Filha();
    }
}

class Pai{

    static {System.out.println("Sou um membro estático da classe pai");}

    public Pai(){System.out.println("Sou o construtor da classe pai.");}

        {System.out.println("Sou um membro de estância da classe pai");}
}

class Filha extends Pai{

    {System.out.println("Sou um membro de estância da classe filha");}

    public Filha(){System.out.println("Sou  construtor da classe filha");}

static{System.out.println("Sou um membro estático da classe filha");} 

}

The exit is:

Sou o método main
Sou um membro estático da classe pai
Sou um membro estático da classe filha
Sou um membro de estância da classe pai
Sou o construtor da classe pai.
Sou um membro de estância da classe filha
Sou  construtor da classe filha
Sou o método main de novo

See running on IDEONE.

References:

http://high5devs.com/2014/12/howto work/

http://www.javaprogressivo.net/2012/10/heranca-de-construtores-e-override.html

http://www.thejavageek.com/2013/07/21/initialization-blocks-constructors-and-their-order-of-execution/

  • What is the order in which they appear?

  • 1

    @bigown in relation to static members, the order in which they appear when the class is loaded in the JVM, and in relation to instance members, are loaded in the order in which they were ordered in the class in which they had an instantiated object. Got too confused as it is in the answer?

  • This was all that was missing. "Trim" is kind of generic.

  • @bigown see if it’s more objective now.

  • A little but not so much, this "appears" seems magical. I can understand, I don’t know if everyone understands what this means. The execution of your code even seems to show that it is not what you described in Marelinho. Then even explains better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.