I have looked at a lot of android tutorials over the internet. In these tutorials they use the this
context for the context everywhere. I know what this
keyword means in Java, but I can't make equal this, with the this
keyword in Android programming. For example, at AlertDialog.Builder
, on the developer.android.com site, there is only one reference at the parameters to the Context, but I can't learn what this this
means here.

- 13,470
- 24
- 79
- 81

- 1,605
- 4
- 21
- 37
-
take a look a this post, it could help you to understand. http://stackoverflow.com/questions/3572463/what-is-context-in-android – JAiro Jun 29 '11 at 12:57
-
2The only thing that came to mind when I saw this question was "What is the meaning of this!" – mark-cs Jun 29 '11 at 13:01
6 Answers
-
. `this` is the current instance of the classe , , it's an instance :) – Houcine Jun 29 '11 at 13:05
If you know "this"
variable.. then u may also know that it holds reference for the current object.. so if some method asks for a Context object you can pass this
variable only when it extends classes like Context or Activity.. Because Activity implements context so obeviously its a Context

- 13,398
- 4
- 44
- 60
Here is another question where they explain Context
. As activities and services extend Context
, the this context is a reference to this activity or service.

- 1
- 1

- 113,398
- 19
- 180
- 268
The keyword this
is simply a reference -- within an instance of a class -- to itself.
There are two common uses of "this" in Android. These aren't particular to Android, but are valid in Java in general.
this.getResources()
says to call thegetResources()
method onthis
instance. Thethis
portion of it is usually unnecessary and used implicitly.Foo.this.getResources()
could be called from an inner class. This could be handy if the inner class definesgetResources()
also, but you want the version defined in the other class.

- 12,072
- 9
- 51
- 69
this
is a reference to the current object
You can use this
to refer to any current instance and any instance of its super class.
If your class extends Activity. It is a case of inheritance. your class is subclass and Activity class is parent class. then you can use this keyword to get instance of Activity beacause Activity class is super class of your class. This is implicit casting
also Activity class Context
as superclass. so you can refer the instance of that class using this keyword.
Thanks

- 53,011
- 55
- 178
- 243