#What does this in this case mean?

1 messages · Page 1 of 1 (latest)

sharp sierra
errant sundialBOT
#

<@&987246399047479336> please have a look, thanks.

sharp sierra
#

for the int ownArea i mean

#

What i have understood is i am currently making a method so when i call this method i will use the object and here this reference to the caller objects variable right?

iron sleet
#

no, this refers to the current instances variables.
Say for that largerThan you passed in (int squares, int rooms) where they were respectively 10, 9.
And you created the instance with 20, 18.

this.rooms would be 20, rooms would be 10.

sharp sierra
#

i think

#

@iron sleet

iron sleet
#

Look at my example with the signature change.

sharp sierra
#

i didn't get what you mean

#

public boolean largerThan(Apartment compared){

iron sleet
#

No.

sharp sierra
#

is only taking Apartment parameter

#

type

iron sleet
#

And my example had different parameters to make it clearer.

#

With this. you refer to the variables of the current object instance.

sharp sierra
iron sleet
#
class Foo {
    int x;

    Foo (int x) {
        this.x = x;
    }
    
    void printFoo(int x){ // this is 10
        System.out.println(this.x); // prints 5
        System.out.println(x); // prints 10
    }
    
    public static void main(String[] args) {
        new Foo(5).printFoo(10);
    }
}
chilly yarrow
#

that code wont compile

sharp sierra
#

gotchu

#

Damn thank you very much