Problems with object references in Java accessor methods

Source: Internet
Author: User

"Note Do not write accessor methods that return references to mutable objects." Because it destroys the encapsulation of a class, the contents of the reference may be changed, resulting in a business logic error.

What is a mutable object?

To understand the problem of value passing and reference passing in Java, let's summarize the following:

1. The object is to pass the reference

2. The original type is the pass value

3.string,integer, double, etc. immutable types because they do not provide their own modified functions, each operation is a new object, so special treatment. Can be considered as a pass value.

Integer is the same as String. The class variable that holds the value is the final attribute, cannot be modified, and can only be re-assigned/generated new object. When an integer is passed into the method as a method parameter, its assignment causes the reference of the original integer to be pointed to the stack address within the method and loses its point to the original class variable address. Any action done on the assigned integer object does not affect the original object.

Therefore, a mutable object is a generic object that is outside the immutable type. [Immutable class: If each method in a class does not change its object, this class is immutable. ]

If you need to return a reference to a Mutable object, first clone it, use. Clone, such as "return Hireday.clone ();", to generate a copy of the object on another memory location.

A lot of people on the "Java programming language always take the call by value" This sentence and the reference pass the misunderstanding, that the contradiction, in fact, no contradiction, when the reference is passed, the method calls the object copy value, is also a reference to the class object, when the method call ends, the copy value is no longer used, The value of the object modified by the copy value is changed during the execution of the method.

Problems with object references in Java accessor methods

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.