• 周五. 12月 2nd, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Find out the problem of class loading in Java at one time

[db:作者]

1月 6, 2022

Abstract : A lot of the time we talk about class loading , People can’t remember the order right away , This article will use an example to clarify many problems of class loading for you at one time .

This article is shared from Huawei cloud community 《 use 1 Add an example 5 A question , Find out all at once java Class loading problem in 【 Run! !JAVA】》, Original author :breakDraw .

A lot of the time we talk about class loading , People can’t remember the order right away , This article will use an example to clarify many problems of class loading for you at one time .

Java Class loading order

quote 1 A classic example from the Internet , And make a little change , So that we can better understand .

The original example is quoted from :https://blog.csdn.net/zfx2013/article/details/89453482

public class Animal {
private int i = test();
private static int j = method();
static {
System.out.println("a");
}
Animal(){
System.out.println("b");
}
{
System.out.println("c");
}
public int test(){
System.out.println("d");
return 1;
}
public static int method(){
System.out.println("e");
return 1;
}
}
public class Dog extends Animal{
{
System.out.println("h");
}
private int i = test();
static {
System.out.println("f");
}
private static int j = method();
Dog(){
System.out.println("g");
}
public int test(){
System.out.println("i");
return 1;
}
public static int method(){
System.out.println("j");
return 1;
}
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println();
Dog dog1 = new Dog();
}
}

Carry out this paragraph main Program , What will be output ?
The answer is
eafjicbhig
icbhig

In order to facilitate everyone to understand the details one by one , I’m going to ask questions in a different way .
Q: When will static variable assignment and static code block execution take place ?
A:

  • Create an instance of a class or subclass of a class for the first time
  • Accessing static variables of a class 、 Calling static methods of a class
  • Using the reflection method forName
  • Call the main Method ( The first static initialization in this example actually belongs to this case , Called Dog Of main Method )
    notes : Class initialization takes place only once , When any of the above conditions are triggered , There will be no class initialization after that .

Q: When initializing a subclass , Will the parent class be statically initialized ? What about the order? ?
A: If the parent class has not been statically initialized before , Then it will go on , And the order is from parent class to child class . The same is true for later non static member initialization .
So it will output eafj.

Q: Why the father of method It won’t be subclassed method rewrite ?
A: Static methods are class methods , Not overridden by subclasses . After all, when a class method is called , It must have a class name .

Q: Why is the first output e instead of a?
A: Because the display of class variables, assignment code and static code block code are executed from top to bottom .
Animal During the static initialization of ,method The call for is in static Before the code block , So first output e Then the output a.
and Dog During the static initialization of ,method The call for is in static After the code block , So first output f, Then the output j

Q: It is not invoked in the constructor of the subclass super() when , Will the instantiation of parent class objects also be carried out ?
A: It will be . The default constructor of the parent class is automatically called . super() It is mainly used in the case of calling the special constructor of the parent class .
So it’s going to start with Animal Object instantiation of , Proceed again Dog Object instantiation of

Q: Construction method 、 Member display assignment 、 Non static code block ( The output c and h the 2 sentence ) What’s the order of ?
A:

  1. Member display assignment 、 Non static code block ( In order of definition )
  2. Construction method
    therefore Animal The output of the instantiation process of icb( If the output is i Have a question , See the following question )
    Then proceed Dog Instantiation , Output hig

Q: Why? Animal Instantiation time , i=test() The output of is i instead of d?
A: Because what you really create is Dog Subclass ,Dog A subclass of test() Method due to signature and parent class test The method is consistent , therefore test Method is overridden .
This is called even in the parent class , Also use subclasses Dog Methods . Unless you new Yes. Animal.

Q: Same as above , If test Methods are private perhaps final attribute , Will the situation of the above question change ??
A:
because private and final Methods cannot be overridden by subclasses .
therefore Animal Instantiation time ,i=test Output d.

Summarize the sequence :

  1. Explicit assignment of parent static variables 、 Parent static code block ( In order of definition )
  2. Explicit assignment of subclass static variables 、 Subclass static code block ( In order of definition )
  3. Explicit assignment of parent non static variables ( Parent instance member variable )、 Parent class non static code block ( In order of definition )
  4. Parent constructor
  5. Subclass nonstatic variables ( Subclass instance member variable )、 Subclass non static code block ( In order of definition )
  6. Subclass constructor .

Class loading process

Q: Class loaded 3 The first necessary stage is :
A:

  1. load ( Class loader reads binary byte stream , Generate java Class object )
  2. link ( verification , Assign static field initial zero )
  3. initialization ( The previous topic is about the order of initialization )
    More details are as follows :

The relationship between passive reference and class static initialization

Q:new An array of classes , Will class initialization be raised ?
Like the output below

public class Test {
static class A{
public static int a = 1;
static{
System.out.println("initA");
}
}
public static void main(String[] args) {
A[] as = new A[5];
}
}

A:
new Array time , Class initialization is not raised .
Output nothing .

Q: Of the reference class final Static field , Will class initialization be raised ?
Like the output below ?

public class Test {
static class A{
public static final int a = 1;
static{
System.out.println("initA");
}
}
public static void main(String[] args) {
System.out.println("A.a=" + A.a);
}
}

A: Not trigger .
No output initA. Get rid of final It will trigger .
( Note that this must be the base type constant , If it’s a reference type , Class initialization is raised )

Q: Subclasses refer to static members of the parent class , At this time, the subclass will do class initialization ?
What will be output as follows

public class Test {
static class A{
public static int a = 1;
static{
System.out.println("initA");
}
}
static class B extends A{
static {
System.out.println("initB");
}
}
public static void main(String[] args) {
System.out.println("B.a=" + B.a);
}
}

A:
Subclasses don’t initialize .
Print initA, But they don’t print initB.

Class loader

Parents delegate

Parent delegation model when class loading , I don’t know how to make a question … Anyway, remember to go to the parent class loader first to see if the class can be loaded .

Just paste a picture :

Be careful , There’s something wrong with the picture above .
Bootsrap No ClassLoader Subclasses of , He is C++ Compiling .
and ExtClassLoader and AppClassLoader It’s all inherited ClassLoader Of

Q:java in , Whether the package name and name of the class and interface are the same , Then it must be the same class or interface ?
A: error .
1 individual jvm in , The uniqueness of classes and interfaces is determined by   Binary name and its definition class loader   Joint decision .
therefore 2 When two different loaders load the same class or interface , It’s actually different .

 

Click to follow , The first time to learn about Huawei’s new cloud technology ~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注