1

Possible Duplicate:
Java this.method() vs method()

I've been reading some things and doing some tutorials about android java but I still dont understand what "this" means, like in the following code.

    View continueButton = this.findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);
    View newButton = this.findViewById(R.id.new_button);
    newButton.setOnClickListener(this);

Also why is it in this example that a button is not defined with Button but with View, what is the difference?

ps. Great site!! trying to learn java and got ALLOT of answers by searching here!

Community
  • 1
  • 1
Tricky
  • 145
  • 1
  • 2
  • 9
  • You can't know. Because `this` is the receiver of the method in which this code is present. As we don't have the code, we can't answer. – Denys Séguret Jun 06 '12 at 19:53
  • 2
    Here's the official Java explanation of [this](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html). – Sam Jun 06 '12 at 19:54
  • @Mark Not necessarily a dupe of that question, since the OP also asks about `setOnClickListener(this)` – Andres F. Jun 06 '12 at 20:00
  • read here: http://en.wikipedia.org/wiki/This_(computer_programming) and then find a good Object Oriented programming tutorial ... – rosco Jun 06 '12 at 20:11

6 Answers6

5

The this keyword is a reference to the current object. It is used to pass this instance of the object, and more.

For example, these two allocations are equal:

class Test{

    int a;

    public Test(){
        a = 5;
        this.a = 5;
    }

}

Sometimes you have a hidden field you want to access:

class Test{

    int a;

    public Test(int a){
        this.a = a;
    }

}

Here you assigned the field a with the value in the parameter a.

The this keyword works the same way with methods. Again, these two are the same:

this.findViewById(R.id.myid);
findViewById(R.id.myid);

Finally, say you have a class MyObject that has a method which takes a MyObject parameter:

class MyObject{

    public static void myMethod(MyObject object){
        //Do something
    }

    public MyObject(){
        myMethod(this);
    }

}

In this last example you passed a reference of the current object to a static method.

Also why is it in this example that a button is not defined with Button but with View, what is the difference?

In Android SDK, a Button is a subclass of View. You can request the Button as a View and cast the View to a Button:

Button newButton = (Button) this.findViewById(R.id.new_button);
nhaarman
  • 98,571
  • 55
  • 246
  • 278
1

this is referring to the instance of the object that is being acted upon.

In the case that you have above, this.findViewById(R.id.continue_button) this is referring to a method in a parent class (Specifically either Activity.findViewById() or View.findViewByid(), assuming you are writing your own subclass of Activity or View!).

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • That code could be placed within a custom View and it would still work perfectly. Saying its only Activity.findViewById() is not perfectly correct. – HandlerExploit Jun 06 '12 at 19:55
  • "this.someMethod" doesn't necessarily call the method in the parent class. In fact, `this` doesn't add information in this case, the way say, `super` would. (Unless by "parent class" you meant "the class of this instance). – Andres F. Jun 06 '12 at 19:57
  • 1
    @HandlerExploit Thank you for pointing that out. I will update accordingly. – nicholas.hauschild Jun 06 '12 at 19:57
0

"this" is the current object instance.

class Blaa
{
   int bar=0;
   public Blaa()
   {}
   public void mogrify(int bar,Blaa that)
   {
       bar=1; //changes the local variable bar
       this.bar=1; //changes the member variable bar. 
       that.bar=1; //changes "that"'s member variable bar. 
   }

}
Mr_Dave
  • 547
  • 1
  • 8
  • 17
Markus Mikkolainen
  • 3,397
  • 18
  • 21
0

this refers to the current instance of a class

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

this in Java is a reference to the current object instance. So if you are writing a method for class MyClass, this is the current instance of MyClass.

Note that in your case, writing this.findViewById(...) isn't really necessary, and may be considered bad style.

Andres F.
  • 403
  • 1
  • 13
  • 24
0

"this" in object oriented languages such as java, c# is a reference to the object on which you are invoking the method or whose data you are accessing.

See if this link is helpful for you in understanding the "this" more -

http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

ksachdeva
  • 91
  • 6