I would seriously recommend reading up a little more on Object Oriented programming... The "this" keyword is an extremely basic and extremely essential concept for OO programming. It is used in Java, C++, C#, and many other OO programming languages. If an OO language doesn't have the "this" keyword it usually has a different keyword like "self" that means and does the same thing.
Much of your confusion is a direct result of not understanding the "this" concept... Once you understand it, many other things will become much more clear. ---------------------------------------------------------------------- There are only 10 types of people in the world... Those who know binary and those who don't. ---------------------------------------------------------------------- On Fri, Jul 16, 2010 at 2:00 PM, DanH <[email protected]> wrote: > setOnClickListener takes a reference (pointer) to an object as its > argument. That object must implement the onClick() method. So when a > click occurs, the onClick method of that object (that specific > instance of that class) is called. Using "this" just directs that the > onClick method of the current object be used if a click occurs. > > Another way to understand "this": > > public class MyClass { > public void myMethod(Object someObj) { > if (someObj == this) { > System.out.println("someObj == this"); > } > else { > System.out.println("someObj != this"); > } > } > > public class MyOtherClass { > public void doSomething() { > MyClass theInstance = new MyClass(); > MyClass notTheInstance = new MyClass(); > theInstance ->myMethod(theInstance); // This will print "==" > theInstance ->myMethod(notTheInstance); // This will print "!=" > theInstance ->myMethod(this); // This will print "!=" > } > } > > On Jul 16, 2:43 pm, Keith Roberts <[email protected]> wrote: > > setOnClickListener actually takes the callback method that will be > > invoked when the user presses the button interface as an argument, so > > it doesn't take a view.... Basically you're saying "this" is just a > > reference to the current object you're in. So if you're in a specific > > method and you use this, it tells the complier to pass in class > > variables not the local ones because the instance of the class would > > be outside of the method?? also, if your class contains multiple > > instances how does the complier know which one "this" is referring > > too? > > > > On Jul 13, 1:47 am, Justin Anderson <[email protected]> wrote: > > > > > *> I know that the keyword "this" refers to an instance variable or can > > > invoke a constructor* > > > That is not what the keyword "this" references. It references the > instance > > > of the class you are currently in... > > > > > *> but I don't understand how passing "this" as the arg for > > > setOnClickListener () works?Could someone explain?* > > > Take a look at the argument list for setOnClickListener... One of the > > > arguments is probably a View. So, when you are calling > setOnClickListener > > > and you are in a class that inherits View, you pass "this" because > "this" > > > is-a View instance. It wouldn't work if you were calling > setOnClickListener > > > from a class that didn't eventually inherit from View. > > > > > Hope that helps... sounds to me like you may want to brush up a little > bit > > > on Java. > > > > > ---------------------------------------------------------------------- > > > There are only 10 types of people in the world... > > > Those who know binary and those who don't. > > > ---------------------------------------------------------------------- > > > > > On Mon, Jul 12, 2010 at 8:50 PM, Keith Roberts <[email protected]> > wrote: > > > > Hey all, > > > > > > I know that the keyword "this" refers to an instance variable or can > > > > invoke a constructor, but I don't understand how passing "this" as > the > > > > arg for setOnClickListener () works?Could someone explain? > > > > > > Thanks, > > > > > > keith > > > > > > -- > > > > You received this message because you are subscribed to the Google > > > > Groups "Android Beginners" group. > > > > > > NEW! Try asking and tagging your question on Stack Overflow at > > > >http://stackoverflow.com/questions/tagged/android > > > > > > To unsubscribe from this group, send email to > > > > [email protected]<android-beginners%[email protected]><android-beginners%2Bunsubscr > [email protected]> > > > > For more options, visit this group at > > > >http://groups.google.com/group/android-beginners?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Android Beginners" group. > > NEW! Try asking and tagging your question on Stack Overflow at > http://stackoverflow.com/questions/tagged/android > > To unsubscribe from this group, send email to > [email protected]<android-beginners%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-beginners?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en

