Java execution order

I am trying to understand this example from "Thinking in Java" :

package c07; import com.bruceeckel.simpletest.*; class Meal { Meal() { System.out.println("Meal()"); } } class Bread { Bread() { System.out.println("Bread()"); } } class Cheese { Cheese() { System.out.println("Cheese()"); } } class Lettuce { Lettuce() { System.out.println("Lettuce()"); } } class Lunch extends Meal { Lunch() { System.out.println("Lunch()"); } } class PortableLunch extends Lunch { PortableLunch() { System.out.println("PortableLunch()");} } public class Sandwich extends PortableLunch { private static Test monitor = new Test(); private Bread b = new Bread(); private Cheese c = new Cheese(); private Lettuce l = new Lettuce(); public Sandwich() { System.out.println("Sandwich()"); } public static void main(String[] args) { new Sandwich(); monitor.expect(new String[] { "Meal()", "Lunch()", "PortableLunch()", "Bread()", "Cheese()", "Lettuce()", "Sandwich()" }); } } 

As I understand from the specification of the Java language, the execution order begins with loading the class containing the main method. Then all statistical and member variables of this class should be initialized (before which all member variables of the superclasses should be informed, although in this case there is none of them).

So, I thought b , c , l would be initialized before main started. However, this is not so. Did I miss something?

+4
source share
3 answers

No, b and c are instance variables.

There is no automatic instance of the class containing main . Only static variables are initialized. It is as if some external caller wrote:

 Sandwich.main(args); 

So when you wrote:

Then all statics and member variables of this class must be initialized

... that was wrong. Only static variables are initialized - as usual.

+4
source

The output example is correct. Here are the important rules:

  • when the class is created, the constructor of the superclass must be called first. It bubbles up to an Object class.

  • Before the constructor is called, member variables are initialized.

In this example, static not involved, except for the technical monitor .

0
source

JLS # 12.4.1. When initialization occurs

The class or interface type T will be initialized immediately before the first occurrence of any of the following values:

  • T is a class and an instance of T. is created.
  • T is a class, and the static method declared by T is invoked.
  • Assigned to a static field declared by T.
  • A static field declared by T is used, and the field is not a constant variable (ยง4.12.4).
  • T is a top-level class (ยง7.6), and the statement (ยง14.10) is lexically embedded in T (ยง8.1.3).

JLS # 12.5. Creating instances of a new class

Whenever a new instance of a class is created, a memory space is allocated for it with room for all instance variables declared in the class type and all instance variables declared in each superclass of the class type, including all instance variables that may be hidden (ยง8.3 )

0
source

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


All Articles