Why set variable equal to instance variable (5. Constructor Parameters)


#1

I am just curious as to why the instance variable (age) is set to equal a new variable/parameter (dogsAge) within the constructor? Why not just use the variable you have already set? I have noticed that if I attempt to just use age instead of dogsAge, it returns 0 rather than the age specified further down. So I'm just confused as to why this is necessary.

class Dog extends Animal {
int age;
public Dog(int dogsAge) {
age=dogsAge;
}

Don't worry about the syntax, this is just a copy-paste of the relevant section.

Edit: verbage


#2

The argument to the constructor doesn't exist in the object, it only exists in the scope of the constructor.

If the object is supposed to remember that value, then you'll need to store it somewhere in the object.