NonameJavaNoob
NonameJavaNoob

Reputation: 35

What is the difference between those 2 methods?

Method number 1:

public void setAge(int newAge){
    age = newAge;
}

Method number 2

public void setAge (int age){
    this.age = age;
}

Upvotes: 2

Views: 122

Answers (6)

Vasile Iordache
Vasile Iordache

Reputation: 1

Both methods are the same, will do same thing, but standard is to use

public void setAge (int age){
     this.age = age;
}

with "this.age" (this-meaning the age of your object) and with the same parameter name as the field.

Upvotes: 0

Muhammad
Muhammad

Reputation: 7324

Both are setter methods which is a common convention to assign values to private variables inside the class, both the methods have the same functionality, the first method is assigning a value to the class field called age while the second is also assigning a value to the same variable but that variable is shaded by the age parameter because their names are the same, so to change the field age instead of the local parameter that is only available inside the function you need to use this.age which means you want to change the field not the local variable.

Upvotes: 1

Loduwijk
Loduwijk

Reputation: 1977

In both, it must be assumed that the object has a field called age.

Second Version

In the second one, there is also a parameter called age. Since the parameter age has the same name as the field age, it "hides" the field. So, within the second version, anywhere you see age will be referring to the parameter, not the field.

Since the field is hidden, you need a way to still access it. You can still get at it by using the this reference. this refers to "this" object, and this.age accesses the field instead of the parameter. So field this.age is set to parameter age.

First Version

The first version just avoids this ambiguity by using a different parameter name so that the field is not hidden, so it still sets field age to the parameter but does not need the this. prefix.

Upvotes: 5

Rahul Kumar
Rahul Kumar

Reputation: 2344

In the first case, it is clear both variables are different, so no problem.

But in the second case which age you are explicitly specifying which age you are referring to and it is a good practice. We add this.age so that compiler knows you are referring to class variable not local variable in left side and right side local variable age

Upvotes: 0

Mikolaj
Mikolaj

Reputation: 43

I think that age is a member of the class you use. In this case in second method the argument got a same name as member class so you need to use this specifier inside a method to specify that you want to assign a new value to a class member.

Upvotes: 0

t-bone
t-bone

Reputation: 1

Considering that setAge function is a member function of a class that has a member variable age: Both codes will change the value of age to the argument passed to the function.

Having said that, second method is considered a good practice where you are explicitly specifying that you want to change the member variable of the current object by using this keyword.

Upvotes: 0

Related Questions