Why use "this" while creating objects?

0 servet portakal · December 31, 2014
In java ( not an expert ) it is used to remind that the variable is local and it is optional.  Here I don't see why we have it, I don't understand the meaning and function of it.   I don't know what elements can be local in Android either.  I need some help :)


EditText userName = new EditText(this);

Also, after this there is not a variable name like this.phoneNumber:  Is it a different usage of "this"?

Post a Reply


Oldest  Newest  Rating
0 Kuroodo Ditory · January 2, 2015
This = This current class.

Let's say you had a class called Banana and a class called Basket.

The Basket class has a constructor and takes in a Banana as a parameter 

Basket(Banana myBanana) {


Let's say you are doing some code inside the Banana class and you make a Basket reference/object in there. In the Banana class you would do: Basket myBasket = new Basket(this).

That's one way to do it. 

Another way would be if you have two variables/objects with the same name. In this case, one of them is a global variable. We will be using a setter message to set the value of that variable.

private int myInt = 5; // Global variable

public void setMyInt(int myInt){
 this.myInt = myInt;

When we said this.myInt, it means the one from this class (the global one). Try testing that code and sing/double click on each variable. That should highlight any instance where the variable is used. Inside the setMtUwill see that the this.myInt would highlight the global variable, and the myInt would only highlight the variable in the method parameters.

Edit: Here is a text file with illustration that I made (I hope this helps anyone else who is confused as well)

0 servet portakal · January 2, 2015
 I've been reviewing my old Java tutorials all day  and I have heard a few new stuff about "this" that I had missed the first time.

I have also read your comment like 50 times and tried to visualize things. So,t hings are much clearer now. Thanks alot.
0 servet portakal · January 2, 2015
Uh, Ok, I understand that.  EditText object is a descent of Context class, because it is in an Activity and Activity class inherits Context class. 

I see, in Android documentation it says:

"Context class allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc. "

Also, I see that EditText requieres a Context object as a parameter.

public EditText (Context context)

But, to be exact, with "this", are we passing our EditText object named userName as a parameter, or are passing some other info about Context class (Because EditText is a descent of Context class, it has that info) ?

Btw, so far we've put everything in an activity, Main Activity that is.  I guess not everything has to be in an activity, because you said:

the code snippet you posted above is probably happening somewhere in an Activity

Thanks a lot.  Although I am familiar with Java, I started programming with C in which you can see where everything is coming from :).  Java/Android codes look pretty long to me, and sometimes I can't see what every single piece of the program doing and what parts am I gonna need to modify in the future etc.
0 servet portakal · January 1, 2015
Thanks for the responses, I still can't seem to be able to connect the dots.  Probably, I didn't phrase the question well. Anyways, I won't get stuck here. It'll probably make sense over time. Thanks again
+1 Developer John · December 31, 2014
You use the this keyword in Java to tell the compiler that you want to use the object that is currently being used or created.
+1 Arshath Shameer · December 31, 2014
As far as i know if u wanna use any component out side and inside of any corresponding method u must declare it as global variable but if u wanna use it inside and don't wanna use it outside of any corresponding method, u can declare it as locally or globally but declaring locally is most preferable since only that particular method gonna use that corresponding component.  

Here we use this "this" to refer the object of that corresponding class, so that it places the component on it. 
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator