Understanding of "instances" in Java

Source: Internet
Author: User

Just take the questions and find the answers.

Is the new object an instance?
Yes, it's an example, you're right. This new object can be called an instance, it is said that the new "thing", called its object can also, call it instance also can, object and instance in this angle is equivalent.

Please look at the code:
Java uses the new keyword and constructs a method to create an object, the following is a class named Cat.
public class Cat {
Public Cat () {System.out.println ("This is a constructor method");}
}


Using the new + construct method to create an object, then
Cat C = new Cat ();


The first half, cat C means to allocate a variable in memory, called C, which is the Cat type, what is its value?
A moment to say;
The second part, New Cat (); This is the new keyword and the constructor method to create an object, Cat () is the name of the constructor, right? Want to create an object, so to write, grammar provisions, no why;
New Cat (); An object in the Cat class is described, and when the program runs, it calls the construction method Cat (), and when the construction method is finished, the cat type object is created and really appears in memory;
Objects created using the New keyword are allocated in the heap of memory (heap), and when the object is actually out, it will do something important:
Our object is allocated in memory, then the memory is large, where is this object? How do we find it? When the new keyword creates an object, it returns the object's address in memory, and the object can be found by this address.
Cat C = new Cat ();
This means that the object in the memory of the address assignment to the variable C, which is the Java reference concept, C is called a reference, or a reference variable, or directly called a variable, no problem, it is;
The value of C is a memory address, or a reference address, through this address, you can find exactly what we have just created the object, we will use this object to do something, call this object method what, have used this reference, OK?
----
Summary c is a reference, not an Object! We're new out of this thing, really in memory of this thing called object, called "instance".

Understanding of "instances" in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.