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

Ronan Corcoran
Ronan Corcoran
878 Points

Why do some keywords like this.variableName have a dot while new ClassName has a space?

I'm unsure of why when creating an object we right it as PezDispenser dispenser = new PezDispenser();

instead of PezDispenser dispenser = new.PezDispenser();

I keep mixing up spaces and dots when using some keywords. I understnad that dots used for calling methods.

but why does this.characterName use a dot and not space.an inversly why is the keyword new follwed by a space and then the class name.

Thanks

Ronan

1 Answer

Steven Parker
Steven Parker
229,708 Points

You nearly answered it yourself when you said "the keyword new". And since "new" is indeed a keyword and not a class instance, it is followed by a space. You do the same thing with other keywords like "class" or "return".

But when you write "this.characterName", "this" is a class instance. So the membership operator (the dot) is used to join it to the name of one of its fields (like "characterName").