What is java instantiation
Persion p1 = new Persion();
Java instantiation is to create an object using the new keyword The process, for example, "new Persion()" means instantiating a Persion object, where the parentheses at the end of Persion mean that the constructor of the Persion class is immediately called to perform the initialization operation. , in fact, it contains four actions. (Recommended tutorial: java tutorial)
1) The "new Persion" on the right is based on the Persion class As a template, create a Persion class object (also referred to as Persion object) in the heap space.
2) The () at the end means that after the object is created, the constructor of the Persion class is called immediately to initialize the newly generated object. There is definitely a constructor. If you don't write it, Java will add a default constructor for you.
3) "Persion p1" on the left creates a Persion class reference variable. The so-called Persion class reference is an object reference that can be used to point to the Persion object in the future.
4) The "=" operator makes the object reference point to the Persion object just created.
Example:
class A { int i; }
Here A is a class
and the object is a specific one of the class, such as
A a1 = new A(); A a2 = new A();
a1 a2 are all objects
And the process of creating an object is called instantiation
So sometimes we also call the object an instance of a class.
The above is the detailed content of what is java instantiation. For more information, please follow other related articles on the PHP Chinese website!