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

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,359 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