Home >Java >Javagetting Started >What does instance mean in Java?

What does instance mean in Java?

(*-*)浩
(*-*)浩Original
2019-11-13 09:42:263824browse

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. .

What does instance mean in Java?

##This way:

Use the new keyword in Java Add a constructor method to create an object. The following is a class named Cat (Recommended learning:

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 second half, new Cat(); This is the new keyword and construction method to create an object, Cat() Is it the name of the constructor method? If you want to create an object, just write it like this. The syntax is stipulated and there is no reason;

new Cat(); It means that new is an object of the Cat class. When the program is running, the constructor method Cat() will be called. , after the execution of this construction method is completed, the Cat type object is created and actually appears in the memory;

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 method

Cat 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what are java eventsNext article:what are java events