Home >Java >JavaBase >java what is an instance

java what is an instance

angryTom
angryTomOriginal
2019-11-20 13:42:065464browse

java what is an instance

#java What is an instance

Instance in java is an object, which is an object of a certain class. A class is just an abstract thing, and objects are real things. So it's called an instance.

We can call the object produced by new an instance. To put it bluntly, it is the "thing" produced by new. You can call it an object or an instance. Objects and instances are equivalent from this perspective. of.

This way:

Use the new keyword plus the constructor method in Java to create an object. The following is a class named Cat,

public class Cat {
    public Cat() {
        System.out.println("这是构造方法");
    }
}

Use the new constructor to create an object, which is the first half of

Cat c = new Cat();

, Cat c means to allocate a variable in memory, named c, this variable is of type Cat .

The second half, new Cat(); This is the new keyword and construction method to create an object. Cat() is the name of the construction method, right? 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 heap area of ​​the memory ( 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, where is this object? How to find it? After the new keyword creates an object, it will return the address of the object in memory. The object can be found through this address. Then our above writing method, Cat c = new Cat();

It means that the address of this object in memory 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 about it ;The value of

c is a memory address, or 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 and call this object's Methods and other things have all used this reference.

Note, let me say it again, many people are not sure 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.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of java what is an instance. 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