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.
Tomasz Kaleta1,554 Points
Why String "before" gives an output "Yoda"?
Overall I understand the lesson. Just curious why this:
String before = dispenser.swapHead ("Darth Vader");
still gives an output "Yoda" when we define in printf method one variable as "before" shouldnt it be "Darth Vader" since this string = swapHead method that changes character name?
Steven Parker216,810 Points
You are quite right that the function changes the character name to "Darth Vader", but inside the function, the old name was saved as "originalCharacterName". That's the value the function returns, even though it has already changed it.
I suspect the variable being assigned was given the name "before" to indicate that it will be storing the name that was used before the switch. This is useful for creating the message that will show both the original name and what it was switched to.