Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Kendhall Lebron Ruiz
Courses Plus Student 1,143 PointsSuper confused about swapHead method in lesson
I don't understand how the method works at all. How is it returning the original when it was literally just swapped? Like for example, "The first input was Yoda, but now it is Darth Vader". What is happening in that code?
2 Answers

KRIS NIKOLAISEN
54,668 PointsThe code for swapHead is as follows
public String swapHead(String characterName) {
String originalCharacterName = this.characterName;
this.characterName = characterName;
return originalCharacterName
}
characterName
is what is passed in. In this case "Darth Vader"
String originalCharacterName = this.characterName;
. This stores the current name in originalCharacterName. In this case "Yoda"
this.characterName = characterName;
This sets the current name to the name passed in. The current name is now "Darth Vader"
return originalCharacterName;
The return value is the stored original name. In the case "Yoda"

John McCracken
757 PointsI see why I was so confused. I somehow missed putting in the line "return originalCharacterName;". Now with that line there, it makes sense. I was also initially confused about which this the "this.characterName" referred to. I had it backwards.
John McCracken
757 PointsJohn McCracken
757 PointsI just had the same question after watching it. Really confused too.