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

Android Build an Interactive Story App (Retired) User Input Getting Text from an EditText

Matthew Francis
Matthew Francis
6,967 Points

"This" KeyWord

What is the difference between:

Toast.makeText(AClassIMade.this,name,"Message",.LENGTH_SHORT).show();

vs

Toast.makeText(this.AClassIMade,"Message",Toast.LENGTH_SHORT).show();

For some reason the second one does not work, any ideas why?

1 Answer

Simple answer Java syntax.

A more elaborated run down on this aspect-
ClassName.this this refers to the current object or instance of the class.

In Java dot operator denotes the hierarchy of things. So for example when we write System.out what is means is that there is a out method within the System class.

so if we write this.ClassName , Java interpreter will treat is as some classname method within the current instance of the class, which is not the case.

Hope that made sense. Happy to further explain if not clear.

Matthew Francis
Matthew Francis
6,967 Points

Thanks for the concise answer!