java class creation and instrument variable initialization and constructor complex questions

Asked 2 years ago, Updated 2 years ago, 31 views

class Base {

    int data = 5;


    Base() {

        System.out.println("Called Base()~");

    }

}

class Derived extends Base {

    int value = 4;


    Derived(){
        System.out.println("Called Derived()~");

    }

}



public class HelloJV {

    public static void main(String[] args) {
        Derived clsDerived = new Derived();
    }
}

Before you start asking a big question

"new class_name();"

If the mechanism by which the syntax operates is interpreted in two stages, the class_name is primarily matched through the new operator Create a class instance in memory (creating means that all members of the class are created, including constructors).

Can we think of it in two stages: finding and calling the constructor specified after the new operator?

Knowledge base underlying thinking: Initializing order of instance variables : Default -> Explicit initialization -> Instance initialization block -> Invoke constructor

Flow of thinking about the progression order of the above code:

Derived object cls Derived is generated by the new operator through a constructor named Derived().

Since the clsDerived object was created, the int-type variable value should be initialized to the default value of 0, explicitly initialized to 4, and then the constructor should be called. - Based on the evidence presented above (primary curiosity)

Debugging confirmed that the default initialization (=0) was performed, but the generator was called without explicit initialization (=4). I wonder what you thought wrong.

If you can't solve the question number two, the creator of the child class is automatically inserted by the compiler at the time the child class is called The phrase "super();" is executed first. There are two questions.

First, we know that when a child class is created, it follows a parent class.

And when you create a class, I think it's accompanied by "creating a class and calling the creator of that class."

Based on this, in order to call "super();", the parent class must be created on the memory firstGo

I think, then, as I said before, it would have been "calling the constructor upon creation", so why is super(); inserted?

Even if it is a structure that calls "super();" late, then the variable int-type data should be already in the default (=0) initialization, but after debugging, the creator of the parent class called super() instead of calling it immediately. Doesn't this go against the order mentioned above and then proceed again?

Speaking of which, I don't think I can understand what I'm curious about.

And when you create a class, members are in memory at the same time, instant variables are initialized by default, explicit initialization, and if there is an instance initialization block, is it right to go through internal initialization by calling the generator designated at the time of creation?

java

2022-09-22 13:46

1 Answers

The important thing is that when Java is compiled, the result is generated by byte code and the result is performed in jvm.

In other words, Java is not about Java, but about jvm and byte code.

Compile with Java and look into the byte code. Then your curiosity will be solved.

The constructor side creates an object and invokes init as shown below.

new java/path/classname
dup
invokespecial java/path/classname <init> ()V

There are few recent Korean books on jvm, but there are a couple of translations in the 2000s.

http://www.yes24.com/Product/Goods/130918?Acode=101 http://www.yes24.com/Product/Goods/3577335?Acode=101

The above link book is recommended as a book that I have read with interest.


2022-09-22 13:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.