The difference between an anonymous object and a non-anonymous object

Source: Internet
Author: User

After the class has been encapsulated, you can create objects by using the New keyword and the constructor of the class, which is divided into two types: Anonymous and non-anonymous objects. What is an anonymous object and what is a non-anonymous object? For example: If you have the following classes:

Package com.heima.vo;

public class student{
Private String name= "Lisi";
private int age=12;
public void print () {

System.out.println (name+ ":" +age);
}
}

This form of object is called an anonymous object: New Student ();

This form of object is called a non-anonymous object: Student stu=new Student ();

The difference between the two:

1, the structure of the two different, this point through its manifestation at a glance, not to repeat;

2. The distribution in memory is different: objects created by anonymous objects exist only in the heap, while objects that are not anonymous objects are also in the heap, but their class variables are in the stack, and class variables in the stack point to the corresponding object by creating the memory address of the variable.

3. ① the Anonymous object invocation method makes sense, and the properties in the calling class have no meaning: for example: New Student (). Name= "Wangwu"; This belongs to calling properties using an anonymous object, so why is it pointless to do that? When the code executes to this line of code, it modifies the name property of the new Student () object, but when the statement is executed? Since new Student () does not continue referencing, this object becomes garbage in the heap. So why do anonymous object invocation methods make sense? method exists in the stack space, when the object that is created in memory calls the method, the JVM begins to open up memory space for the local variable, the method begins execution, the method emphasizes its function, and the object invocation method can complete its function, so the anonymous object invocation method makes sense.

② a non-anonymous object invokes a method or invokes a property in a class that has meaning (where the former is not described, see point ①): For example: Student stu=new Student (); Stu.name= "Wangwu" This belongs to calling properties using non-anonymous objects. So why does that make sense? When the code executes to this line of code, the property of the Stu object is modified, and the print () method is used to invoke the output name value of Wangwu, which is why it is considered a non-anonymous object to invoke the property for practical purpose.

The difference between an anonymous object and a non-anonymous object

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.