📜  “new”关键字的使用——无论代码示例

📅  最后修改于: 2022-03-11 14:59:50.859000             🧑  作者: Mango

代码示例1
When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. 

Declaration : First, you must declare a variable of the class type. This variable does not define an object.

Instantiation and Initialization : Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator instantiates a class by dynamically allocating(i.e, allocation at run time) memory for a new object and returning a reference to that memory. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated. 

The new operator is also followed by a call to a class constructor, which initializes the new object. A constructor defines what occurs when an object of a class is created. Constructors are an important part of all classes and have many significant attributes.

Example
Let us say that we have a class called Student and we need to instantiate this class. The following syntax does that : 

Student anil = new Student();