简体   繁体   English

在对象创建过程中到底发生了什么? (爪哇)

[英]What exactly happens during object creation? (Java)

I've been studying from Head First Java and I'm currently on the constructors chapter.我一直在学习Head First Java ,目前正在学习构造函数章节。 They've explained that an object is created by calling a constructor after the new keyword.他们已经解释过,通过在new关键字之后调用构造函数来创建对象。

My question: What is it exactly that runs when you create a new object?我的问题:创建新对象时运行的到底是什么?

Let's take the following as sample code:让我们将以下内容作为示例代码:

public class Const {

    // instance variables
    int number;
    String name;


    // Constructors
    public Const() {
        //implicit super
        System.out.println("no-arg constructor");
    }
    public Const(int i, String s) {
        //implicit super()
        number = i;
        name = s;
        System.out.println("two-arg constructor");
    }


    // test method
    public void doSomething() {
        System.out.println("I'm a test");
    }


    // Main
    public static void main(String[] args) {

        Const c1 = new Const();
        Const c2 = new Const(5, "Jerry");
    }
}

Now here is what I believe is happening under the hood when creating c1 :现在这是我认为在创建c1时正在发生的事情:

  1. public Const() is called public Const()被调用
  2. super() is called super()被调用
  3. the Object part of Const is built.构建了 Const 的Object部分。
  4. public Const() is back on top of the stack. public Const()回到栈顶。
  5. Now here comes my trouble.现在我的麻烦来了。 When and how exactly are the instance variables and methods for the object created (what implicit code is added)?对象的实例变量和方法是何时以及如何创建的(添加了哪些隐式代码)?

What exactly happens during object creation?在对象创建过程中到底发生了什么? (Java) (爪哇)

  • Constructor being called, which will call the super method of the parent Class(By Default Object class, so the new Class will have access of all members of Object class)被调用的构造函数,会调用父类的super方法(默认为Object类,所以新的Class可以访问Object类的所有成员)
  • Memory Allocation内存分配
  • Fields are initialized (by assigned value or if not given then by default value)字段被初始化(通过赋值或者如果没有给出则默认值)
  • rest code block of the constructor will be executed将执行构造函数的其余代码块

So the only difference here is:所以这里唯一的区别是:

Const c1 = new Const();

Here number will be initialized by int default value which is 0 and name will be initialized by null , which is the default value of String这里 number 将被初始化为 int 默认值0和 name 将被初始化为null ,这是 String 的默认值

Const c2 = new Const(5, "Jerry");

Instead here it will be 5 and Jerry respecively相反,这里将分别是 5 和 Jerry

With the new Const() , the constructor will be called and the class will be loaded to the main memory.使用new Const() ,将调用构造函数并将类加载到主内存中。

public Const() {
        //implicit super 
        super()
    }

The super keyword in java is a reference variable that is used to refer parent class objects, which is by default Object class. java中的super关键字是一个引用变量,用于引用父类对象,默认为Object类。 The keyword “super” came into the picture with the concept of Inheritance.关键字“super”带着继承的概念出现了。

So now it has access of all members of Object class ie toString, equals :所以现在它可以访问 Object 类的所有成员,即 toString, equals :

as you can access你可以访问

c1.toString() or c1.equals(obj)

When and how exactly are the instance variables and methods for the object created (what implicit code is added)?对象的实例变量和方法是何时以及如何创建的(添加了哪些隐式代码)?

I think now you understood how instance variables are created, and the default values are assigned to them based on the type.我想现在你明白了实例变量是如何创建的,默认值是根据类型分配给它们的。

If still not clear, try to understand the internal arch, that might help:如果仍然不清楚,请尝试了解内部拱门,这可能会有所帮助:

JVM Arch JVM 架构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中实例化对象时,究竟发生了什么? - What exactly happens when an object is instantiated in Java? Java中的对象创建语句到底意味着什么? - What does the object creation statement in java exactly means? 对象创建期间的java覆盖 - java override during object creation Java:在创建时将值传递给对象时,应该被初始化的成员会怎样? - Java: What happens to the supposed-to-be initialized members when passing a value to their object at creation time? 如果两个对象引用指向同一个可序列化对象,在 Java 序列化过程中会发生什么? - What happens during serialization in java, if two object refrences are pointing to the same serializable Object? 这种方法究竟发生了什么? - what exactly happens in this method? 当为数组和对象调用new运算符时到底发生了什么? - what exactly happens when new operator is called for array and object? 当你声明一个对象时,堆上到底发生了什么? - What exactly happens on the heap when you declare an object? 在Java中为Array中的元素分配引用时到底发生了什么? - What exactly happens while you assign a reference to an element in Array in Java? 当线程进入 Java 中的同步块/方法时究竟会发生什么 - What exactly happens when thread enters a synchronized block / method in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM