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

Rhythm Lama
Rhythm Lama
2,067 Points

in the field where this.characterName = characterName is written what does equals to characterName mean or represent

I did not understand what the "characterName" was calling or is related to in this particular scenario.

2 Answers

Brendon Butler
Brendon Butler
4,242 Points

At around 5:00 in the video he explains it and how to differentiate between the two similar variables.

Basically, the this.characterName variable refers to the class level variable defined outside of any method bodies, towards the top of the file.

The characterName variable without the this. notation refers to the variable passed in to the PezDispenser constructor method.

I think the best way of looking at this kind of question, is that this is an assignment statement. When we have a single equal sign, we are assigning a value to something else.

Think of early days of Algebra.

The example, x=5 is often read Let x be equal to 5, or another way it is read is Set x equal to 5

(Algebra was invented long ago in the Eastern hemisphere and in an Arabic Language. al-jabr is the origin of algebra.
Now - Arabic is read, and written right to left. So if you think about it - we take the value on the right and pass it into the value or variable on the left. Hence we assign values right to left.

So we take the value of characterName that was passed as an argument of the constructor, take that value and set it to the instance or local variable of the object we are creating.

The *this8 keyword simple helps to clarify which is the parameter being passed and which is the name of the object's variable.

Hope this helps