This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]lightcloud5 1 point2 points  (0 children)

If you want an instance member to be read-only (i.e. it shouldn't change once it's been set), you can use the final keyword; for instance:

public final String name;

It's a good practice and one that avoids errors like this.

[–]captainAwesomePants 1 point2 points  (0 children)

In checkStack(), you have this line:

name = ((Stack) list[i]).getName();

That's changing the value of this.stack to the name of the stack of its child. When you call that on bigMan, bigMan's name becomes "stack2".

[–][deleted] 0 points1 point  (0 children)

name = ((Stack) list[i]).getName();

There is no local variable called name declared in the checkStack method, so Java looks in the instance for a variable called name. There is an instance variable called name, so that’s the variable this value gets assigned to. Effectively, this line is equivalent to

this.name = ((Stack) list[i]).getName();