-2

I am a beginner in android and i know some java and basics of programming logic itself

I know there is inheritance in the code of my application

but how exactly does using the keyword "this" work?

and why do i have to pass "this" while i create a new intent

Thank you

  • 7
    You need to learn Java. – SLaks Aug 09 '13 at 13:47
  • http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – SLaks Aug 09 '13 at 13:48
  • I know about "this" keyword but i just needed to get it clear in my head should i learn java and then go on to android? because that doesn't workout for me sometimes i lose my path What would you recommend ? – bharat narang Aug 09 '13 at 13:59
  • duh what are you thinking? How can you program in Android when you don't the basics of Java or even OO programming languages. Android is the advanced implementation of Java concepts so its better to get a grip of those concepts first before applying them in android development – Umer Farooq Aug 09 '13 at 14:13
  • @Umer i know the concepts of OO languages , i know about how objects/instances work.. but i am not perfect with with advanced topics of java . so do i really need to know all of that to start developing android ? I mean i can go back to java if i need to .. – bharat narang Aug 09 '13 at 14:16

3 Answers3

1

"this" is related to java, which android is based upon: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

basically, using "this" means that you wish to refer to the current instance of the class.

it's useful in order to make sure you refer to the fields of the object and not other things, like the temporary variables (or parameters) that are defined in the function.

it's also useful for when you use nested or anonymous classes

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

this is a keyword used by the OO programming languages to refer to the current class. It implicitly fetches the the reference or the address of the object or instance of the current class and passes it to the method you are providing it to as an argument.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
0

'this' refers to the current instance, for example, if on Android you are writing code on for an activity named Funny_Activity, by saying 'this' on the code refers to the current instance of Funny_Activity. But if you create a local class in the activity, then write 'this' inside the local class, then it refers to 'this' local instance class, not the activity, only when you write 'this' outside the local class, will this refer to the activity again.

Imagine, this as the current activity/class you're working on.

  • Please correct me if im wrong.. I can directly access the material in the local class from Funny_activity? and how do i access data from Funny_Activity from the local class – bharat narang Aug 09 '13 at 14:22
  • You would have to declare a variable in Funny_Activity `final Context CONTEXT = this;` then when you want to refer to Funny_Activity in your local class, then you would use 'CONTEXT' instead of 'this' in your local class. Which will refer to Funny_Activity, not the local class. – SeeYouSpaceCowboy Aug 09 '13 at 14:35
  • You would instantiate your local class in Funny_Activity to get access to it. And local classes have access to your Funny_Activity. – SeeYouSpaceCowboy Aug 09 '13 at 14:38