Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Meet Objects Final

Ahmad Khaled
Ahmad Khaled
13,026 Points

SwapHead by Chris!

I don't understand what he declared about Chris changing variables, it's just very confusing!

especially this code block, i can't follow along with Craig anymore.

Public String swapHead(String characterName) {
String originalCharactername = this.characterName;
this.characterName = characterName; 
return originalCharactername;
}

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

this block is called on a character object and is used to give it a new name and then return the old name. the current name is stored, the object is given the new name that is passed in as the argument, and the old name is returned. also, to format code use the backtick (`) not the single quote (').

I still don't get it, this is very very confusing. Please help me.

Original value of this.characterName is stored in the originalCharacterName as a sort of temporary variable. Then he's setting a new value ie characterName which is coming from the argument, to the private member variable this.characterName. In the end, he's returning originalCharacterName because he wants to return the previous value of the private member variable.