简体   繁体   English

对象什么时候初始化?

[英]When exacty is the object initialized?

I want to ask you when is the exact moment when an object is initialized我想问你一个对象初始化的确切时刻是什么时候

For example I have this simple Java code:例如,我有这个简单的 Java 代码:

public class Test {
   public static void main(String args[]) {
      Student student = new Student();
      student.setName("John");
      student.setId(123);
   }
}

So when exactly is the student object initialized?那么student对象究竟是什么时候初始化的呢? Is it initialized when new Student() is executed?是否在执行new Student()时初始化? Or when Student student = new Student() is executed?或者当Student student = new Student()被执行时? Or after setters are executed?还是在执行 setter 之后? Any feedback will be appreciated!任何反馈将不胜感激!

It's initialized when new + constructor is called.它在 new + 构造函数被调用时被初始化。

As states the docs正如文档所述

Each of these statements has three parts (discussed in detail below):这些陈述中的每一个都包含三个部分(在下面详细讨论):

Declaration : The code set in bold are all variable declarations that associate a variable name with an object type.声明:粗体代码集都是将变量名与对象类型相关联的变量声明。

Instantiation : The new keyword is a Java operator that creates the object.实例化: new 关键字是创建对象的 Java 运算符。

Initialization : The new operator is followed by a call to a constructor, which initializes the new object.初始化: new 运算符之后是对构造函数的调用,该构造函数初始化新对象。

Is it initialized when new Student() is executed?是否在执行new Student()时初始化?

Yes.是的。 When the resulting object is returned, it has been initialized (by the constructor code).当返回结果对象时,它已被初始化(由构造函数代码)。

Or after setters are executed?还是在执行 setter 之后?

The fields of the object are initialized by the time the constructor returns.对象的字段在构造函数返回时初始化。 They may be set to null or "" or 0 or similar, but they're initialized with some value.它们可能被设置为null""0或类似的值,但它们是用某个值初始化的。

If it's not valid for a Student object to have null or whatever for those fields, then the constructor should accept the values for them as parameters, or the class should expose a builder-style interface for building an instance, so that by the time you have a Student instance, you know the fields are filled in with meaningful values.如果Student对象对于这些字段具有null或任何其他内容是null ,那么构造函数应该接受它们的值作为参数,或者该类应该公开用于构建实例的构建器样式的接口,以便到您有一个Student实例,你知道这些字段用有意义的值填充。 Whether that's necessary is domain-specific.是否有必要是特定于域的。

It depends on your constructor class.这取决于您的构造函数类。 Probably line three, but if your mutator methods create new objects then line 4 and 5.可能是第 3 行,但是如果您的 mutator 方法创建了新对象,那么第 4 行和第 5 行。

It get initialized when new Student() is executed.它在执行new Student()时被初始化。

  • new Student() - this calls the constructor of the Student Class. new Student() - 这会调用 Student 类的构造函数。

After that, the resulting object is assigned to the ' student ' variable where the data type is Student .之后,将生成的对象分配给数据类型为Student的“ student ”变量。

Whenever ' new ' keyword is invoked, the object is instantiated and the constructor is called resulting in object initialization.每当调用' new '关键字时,对象都会被实例化并调用构造函数,从而导致对象初始化。

First of all, see the difference between a setter and constructor and their pros and cons.首先,看看setter 和constructor 之间的区别以及它们的优缺点。

Constructor:构造函数:

1) A constructor is called when an object is created. 1) 创建对象时调用构造函数。

2) They are only called once per object. 2) 每个对象只调用一次。

3) You may use the constructor to set values at the point of instantiation. 3) 您可以使用构造函数在实例化点设置值。

4) It does not allow any return type. 4) 它不允许任何返回类型。

5) A constructor is invoked implicitly. 5) 隐式调用构造函数。

Setter:二传手:

1) A setter is called to change the value of the object after it was initialized. 1)在对象初始化后调用setter来改变对象的值。

2) A setter can be called any number of times. 2) 一个 setter 可以被调用任意次。

3) You may use the setter to set values after the point of instantiation. 3) 您可以使用 setter 在实例化点之后设置值。

4) It allows return type. 4) 它允许返回类型。

5) A setter is invoked explicitly. 5) 显式调用 setter。

Conclusion:结论:

1) Object is initialized when new Student() is executed 1) 对象在执行 new Student() 时被初始化

2) Use constructor if you think initialization is mandatory and you have the required values before you can use the object. 2) 如果您认为初始化是强制性的,并且在使用对象之前拥有所需的值,请使用构造函数。

3) Use the setter method when the initialization of the variable is non-mandatory and you don't have the values at the time of object initialization. 3)当变量的初始化是非强制的并且你没有对象初始化时的值时,使用setter方法。

4) Generally, we should avoid setter as it somehow violates the principle of encapsulation. 4) 一般来说,我们应该避免使用 setter,因为它在某种程度上违反了封装原则。

Student student = new Student();学生学生=新学生();

When you call new and then the constructor you are creating an object of the same type of your constructor class, the object that is receiving the new + constructor must be the same type.当您调用 new 然后调用构造函数时,您正在创建与构造函数类相同类型的对象,接收 new + 构造函数的对象必须是相同的类型。

Another example:另一个例子:

Object that must be the same as your class + name of the object signal = then new plus constructor;必须与您的类相同的对象 + 对象的名称信号 = then new 加构造函数;

Example exaple = new Example();

When you call你打电话时

Student student = new Student();

you are creating an object in memory.您正在内存中创建一个对象。 Once created, you can use the object's methods, like getters and setters.创建后,您可以使用对象的方法,例如 getter 和 setter。

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

相关问题 Java对象何时完全初始化? - When is a Java object fully initialized? 初始化对象时,实例变量是否始终未初始化? - When Initializing object, instance variable not initialized always? 在另一个方法中初始化对象时出现NullPointerException - NullPointerException when object is initialized in another method 创建对象时可以初始化最终变量吗? - Can a final variable be initialized when an object is created? Java初始化系统class的出object是什么时候? - When is the out object of the System class initialized in Java? 访问初始化对象的内部Object属性时发生NullPointerException - NullPointerException when accessing an internal Object attribute of an initialized object Mockito:@Mock对象何时初始化以及它调用的构造函数 - Mockito: When is @Mock object get initialized and which constructor it calls 通过反射创建对象时,未初始化最终布尔字段 - Final boolean field not initialized when object is created via Reflection 父类初始化时创建子类对象 - Create Sub-Class Object when Parent Class is initialized 使用Spring Dependency注入初始化对象状态时表示关联 - Representing Associations when object state is initialized using Spring Dependency injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM