Reputation: 121
I create a method that casts an aging spell upon people and decreses their age by two:
public class Wizard {
void agingSpell (Person x) {
x = new Person(x.age);
x.age -=2;
}
}
Here's the class describing those people:
public class Person {
int age;
int weight = 90;
String name = Max;
Person (int x){
this.age = x;
}
}
I create an instance of Person and Wizard:
Person max = new Person(26);
Wizard albus = new Wizard();
Then, I call the method agingSpell and source max as its argument:
albus.agingSpell(Person max);
Then, as I see it, the reference value inside max is assigned to x inside the method:
Person x = max;
Now we have one more reference to the created object. Next, a new object is created and (again, I might be wrong), is saved inside the x:
x = new Person(x.age)
I understand the old object to be substituted by the new one, so there has to be no trace of the old one inside the method. BUT, if I compile the code the age of the new object would also be 26 . Furthermor, I can easily access all the other fields of the older object (which was supposed to be unaccessible the moment we assigned his x reference to another object). I know I'm definitely missing something. Could you, please, help me figure it out?
Here's the executing portion of the code:
public class Wizard {
}
public static void main (String [] args){
Wizard albus = new Wizard();
Person max = new Person(26);
albus.agingSpell(max);
System.out.println(max.age);
}
}
Upvotes: 0
Views: 677
Reputation: 78985
Define agingSpell
as follows:
void agingSpell(Person x) {
x.age -= 2;
}
-OR-
Return the new object you are creating. Note that the scope of the new object you are creating inside agingSpell
is limited to this method only i.e. as soon as the control comes out this function, the new object will cease to exist.
class Wizard {
Person agingSpell(Person x) {
x = new Person(x.age);
x.age -= 2;
return x;
}
}
class Person {
int age;
int weight = 90;
String name;
Person(int x) {
this.age = x;
}
}
public class Main {
public static void main(String[] args) {
Wizard albus = new Wizard();
Person max = new Person(26);
System.out.println(albus.agingSpell(max).age);
}
}
Upvotes: 1
Reputation: 53538
You reassign x
in the spell method, so it points to the new person, then you change that new person's age, and then you throw that person away. So the net result is nothing changed. It's the same as doing this:
void agingSpell(Person p) {
Person throwaway = new Person(p.age);
throwaway.age -= 2;
// when this method returns, "throwaway" literally gets thrown away.
}
A better question is "why are you generating a new person at all, if the intent is to decrease a person's age?". Have the spell directly update the person you pass in:
class Wizard {
final int DEFAULT_AGE_DECREASE = 2;
...
void agingSpell(Person p) {
this.agePerson(p, DEFAULT_AGE_DECREASE);
}
void agingSpell(Person p, int years) {
// let's keep a person's age a protected field
p.setAge(p.age - years);
// and you'll probably need logic for age hitting/dropping below zero
}
...
}
Upvotes: 2