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 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
216,121 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").