Java Basic Knowledge _ object

Source: Internet
Author: User

Object can be passed as a parameter to a method

Instead of passing the value of the object itself to the parameter, the passed object passes the reference value of the object to the parameter

void method1 (num num) {

num.i+=10;

}

Any changes in the contents of the Method1 method in vivo are also changes to the object itself

void method2 (num num) {

Num=new Num ();

num.i=100;

}

The object content here hasn't changed.

Because an object in Java is passed as a parameter, it is equivalent to copying a copy of the object's in-memory address to a parameter

Method1, because the formal parameter and the object have the same memory address, so the modification of the formal parameter, the natural object itself content also changed.

And in Method2, just as in Method 1, they still point to the same memory address, but new Num (), which opens up a new area of memory

This causes the shape to participate in the object storage address is not the same, natural changes in the parameters will not be reflected on the object.

This

Represents a reference to the current object

1. Return the current object, or pass the current object as a parameter to another method

Classname method () {

return this;}

2. If the local variable has the same name as the attribute member variable of the owning class (when the property is encapsulated)

This.name=name;

3. Another constructor method that calls its owning class in a constructor method

However, it can be called only once and before any statements in the method body

Final

A final defined object can only point to a unique object , and it cannot be pointed to another object

But the value of an object itself can be changed, except that the object reference (point) is immutable.

Java Basic Knowledge _ 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.