Alias Behavior in Java

Source: Internet
Author: User

 Public classbeiming {/**     * @paramif an instance of a class is directly connected by an equal sign, then two instances point to the same object.
* This way they call the property, or the method will always point to the left instance. The change will fail.
*
* The workaround is to call the property when the attribute is equal and then connect it with an equal sign so that the above behavior does not occur.
   
* See examples
*/
floatTom;
Public Static voidMain (string[] args) {beiming B1=Newbeiming (); Beiming B2=Newbeiming (); B1.tom= (float) 12.00; B2.tom= (float) 345.00; System.out.println ("b1:tom=" +b1.tom+ "\ n" + "b2:tom=" +b2.tom); //B1 = B2; B1.tom=b2.tom;//Solution System.out.println ("b1:tom=" +b1.tom+ "\ n" + "b2:tom=" +b2.tom); B2.tom= (float) 34355.00; System.out.println ("b1:tom=" +b1.tom+ "\ n" + "b2:tom=" +b2.tom); }}

Aliasing occurs when an object is passed into a method

 Public classbeiming {Static voidf (letter Y) {//creates a new static method that assigns the properties of the passed-in objectY.woca = "Static"; }     Public Static voidMain (string[] args) {Letter L=NewLetter ();//Create a new objectL.woca = "Woca attribute of the newly added instance";//assigns a value to the object.System.out.println (the content of the Woca property is: "+L.woca); F (l); //call the method, put in the new object, and then output, it will be found that the reference point has changed, this need to avoid the main.System.out.println (L.woca); }}classletter{//new class for beiming class testsString Woca;}

Alias Behavior 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.