aliasing, mutual assignment between Java objects

Source: Internet
Author: User

Take a look at the code

Import java.util.*;

Class book{

static int c = NULL;

}

public static void Main (string[] args) {

Book Book1 = new book ();

Book Book2 = new book ();

book1.c = 6;

book2.c = 4;

Book1 = Book2;

}

Above, when Book1 and Book2 two objects are assigned to each other, then when we call BOOK.C, we will find that book1.c and book2.c are equal

Cause: We operate directly on the object, because the reference to the object of Book1 and Book2 is the same object, so there is a special appellation, called, alias phenomenon

So how do we expect the two variables to be independent of one another? We should use this, as follows:

book1.c = book2.c;

Thus, the properties of different objects can be assigned to each other, because the properties of different objects are independent of each other

aliasing, mutual assignment between Java objects

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.