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.

Java Java Objects Meet Objects Final

lowell seitz
lowell seitz
631 Points

Why does java "return" statement variable name that it will be returning not have to match the expected variable name i

Why does java "return" statement variable name not have to be the same expected variable name in the method name. What happens when you return the orginalCharacterName, but the method is expecting CharacterName as the return argument/variable. public String swapHead(String characterName) { String originalCharacterName = this.characterName; this.characterName = charaterName; return originalCharacterName; }

2 Answers

Fahad Mutair
Fahad Mutair
10,358 Points

hi lowell seitz ,

public String swapHead(String characterName)

(String characterName) this is the parameter not the return and when you call method with argument it will be set to this parameter

public String swapHead < the string word here means it should return String variable that's why we typed this line return originalCharacterName; and it already declared as String at this line String originalCharacterName

lowell seitz
lowell seitz
631 Points

Thank you I understand now