[英]What exactly happens when an object is instantiated in Java?
I know that when creating an object of a class the constructor builds that object. 我知道在创建类的对象时,构造函数会构建该对象。 Say I had these two class:
说我有这两堂课:
class Vehicle {
public int a = func();
public int func() {
System.out.println("9");
return 9;
}
}
class Car extends Vehicle {
public static void main(String[] args) {
Car c = new Car();
}
}
The output of this project is "9". 该项目的输出为“ 9”。 But why does that happen?
但是为什么会这样呢? What exactly happens when the Car constructor is called?
调用Car构造函数时会发生什么? I know that there is some type of default constructor but I am not sure how it exactly works.
我知道有某种类型的默认构造函数,但是我不确定它是如何工作的。
Can anyone explain me the object construction with the above example? 谁能用上面的例子解释我的对象构造?
The compiler provides a default constructor for Car
which is effectively: 编译器为
Car
提供了一个默认的构造函数 ,该构造函数实际上是:
Car() {
super();
}
And likewise Vehicle
as a default constructor of: 同样,
Vehicle
作为以下内容的默认构造函数:
Vehicle() {
super();
}
Fields are initialized as part of initialization, after the call to the superclass constructor. 在调用超类构造函数之后,将字段初始化为初始化的一部分。 So it's as if the
Vehicle
class was actually written like this: 因此,好像
Vehicle
类实际上是这样编写的:
class Vehicle {
public int a;
Vehicle() {
super();
a = func();
}
public int func() {
System.out.println("9");
return 9;
}
}
Now does that makes sense? 现在有意义吗?
See section 15.9.4 of the Java language specification for a much more detailed description. 有关更详细的描述,请参见Java语言规范的15.9.4节 。
When the Car constructor
is called a default call to it's super constroctor is made by the compiler which then intializes all the Super class's instance fields
. 当
Car constructor
被调用时,其超级构造函数将由编译器进行默认调用,然后编译器会初始化所有Super class's instance fields
。 during the initialization of your a field
as it invokes func()
method which has a sysout thus it prints 9. 在
a field
初始化期间,因为它调用具有sysout的func()
方法,因此将输出9。
public Car() {
super();// this invokes parent class's default cons
}
public Vehical() {
super();// invokes Object constructor then initializes all the instance variables of class vehical
}
Car
must be constructed. Car
。 Vehicle
and therefore triggers a Vehicle
construction. Vehicle
,因此触发了Vehicle
构造。 Vehicle
requires the initialisation of the int a
instance variable. Vehicle
的构造需要初始化int a
实例变量。 int a
variable must be initialised to be the result of the call to func
so func
is called. int a
变量必须初始化是调用的结果func
所以func
被调用。 func
has been called so the code prints "9" . func
已被调用,因此代码显示为“ 9” 。 I think you are misunderstanding the meaning of Car c = new Car();
我认为您误解了
Car c = new Car();
的含义Car c = new Car();
. 。
That statement creates an object reference, and that object's reference is c
. 该语句创建一个对象引用,并且该对象的引用为
c
。 The statement new Car();
声明
new Car();
creates an object and the reference of that object is sent stored in c
. 创建一个对象,并将该对象的引用存储在
c
发送。
When an object is created it initiates all instance variables present in that object.(to its variable's default value if no assignment is given). 创建对象时,它将初始化该对象中存在的所有实例变量(如果未指定赋值,则返回其变量的默认值)。
If a default constructor of a particular class( Car ) is called, a call to the default constructor of its super class( Vehicle ) is made first. 如果调用了某个特定类( Car )的默认构造函数,则将首先对其父类( Vehicle )的默认构造函数进行调用。 And while creating Vehicle object its instant variable "a" is assigned to function func() and it ll be executed, Hence printing 9.
并且在创建Vehicle对象时,将其即时变量“ a”分配给函数func()并将其执行,因此输出9。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.