2
public class A extends B {
    public static final int CONST = 6;

... some logic ...
}

public class C {
    private int addNumber(int x) {
        return x + A.CONST;
    }
}

I wonder if a jUnit Test for class C will just load the field from class A, it depends on or if all the logic from class A with its extension (class B) will be fully loaded.

How does the JVM is working in this case ?

Thanks !

e-gotrip
  • 31
  • 3
  • In the example code, it's possible that class A is not loaded. If the compiler decided to inline the `A.CONST` value into class C (which he is allowed to do because of the `public static final` modifiers), at runtime Class C has no reference to class A, so the JVM has no reason to load class A. – Ralf Kleberhoff Aug 23 '17 at 18:27

1 Answers1

0

On first reference to class A, the class will be fully loaded. Given that A extends B, the class B will be fully loaded as well. Then all static initializers and static fields in B will be executed/initialized in the order specified in B, then in A, and you will return to the code extracting the CONST field.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44