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 Constructors

Confused about constructer scope order

In the constructer in line 4, before he started talking about naming multiple objects the same thing, (around 5:00ish) the string "name" isn't defined, so how did it compile? Or was it defined and I missed it?

1 Answer

Steven Parker
Steven Parker
229,920 Points

Before 4:30, "name" was defined as the parameter on line 4, and it was used on line 5.

Both the parameter and the reference get changed to "characterName" between 4:30 and 4:55. In between those two changes, the code would not compile. But no attempt to compile is made while the editing is incomplete.

So if he had tried to compile it before the change to 'this.characterName' it would not have compiled because 'name' hadn't been declared or defined before it was being used as a definition for 'characterName', but since he didn't try to compile it there and instead fixed the issue before it came up the compilation problem never came up. Am I understanding correctly?

Steven Parker
Steven Parker
229,920 Points

Right. in that section between 4:30 and 4:55 changes are still being made and the code isn't ready to be re-compiled yet.

Got it, thank you very much!