Home >Java >Javagetting Started >What does instance mean in Java?
The new object in Java is called an instance. To put it bluntly, it is the "thing" that comes out of new. You can call it an object or an instance. Objects and instances are equivalent from this perspective. .
##This way:
java course)
public class Cat { public Cat() { System.out.println("这是构造方法"); } }Use the new constructor method to create an object, then that is
Cat c = new Cat();In the first half, Cat c means to allocate a variable in memory named c. This variable is of type Cat. What is its value?
I’ll talk about it later;
The object created using the new keyword is allocated in the memory Heap area (heap), and after the object actually comes out, it will do an important thing:
Our object is allocated in memory, so the memory space is large, this Where is the object? How to find it? After the new keyword creates an object, it will return the address of the object in the memory. The object can be found through this address. Then our above writing methodCat c = new Cat();means that the object is stored in the memory. The address in is assigned to variable c. This is the concept of reference in Java. c is called a reference, or a reference variable, or a variable directly. No problem, it’s all; the value of c is a memory address , or called a reference address. Through this address, we can accurately find the object we just created. In the future, we will use this object to do something, call methods of this object, etc., and we will use this reference, okay? Note, I say it again, many people are confused whether this c is an object or a reference. Many people say that c is an instance of the Cat class. This is very wrong. c is a reference, not an object. ! The thing we created with new is actually called an object or instance in memory.
The above is the detailed content of What does instance mean in Java?. For more information, please follow other related articles on the PHP Chinese website!