0

Possible Duplicate:
What is the meaning of “this” in Java?

I'm still very new to learning Android programming, and I noticed that "this" was used often in parameters for method calls in the language. I'm following The New Boston tutorials through YouTube, but he never really explains quite detailed enough what the 'this' statement means. Can somebody please explain it to me? Maybe dumb it down a bit?

Community
  • 1
  • 1
AlleyOOP
  • 1,536
  • 4
  • 20
  • 43
  • 2
    Hi - 1) just about everything in Java is a "class". 2) "this" is a Java keyword for "this class instance". 3) In other words, "this" passes *itself* as an argument. "This" is a "Java thing"; not just an "Android thing". Here's a good discussion: [Using the this keyword](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) – paulsm4 Dec 25 '12 at 20:17
  • @paulsm4 : Actually `this` is used in other OO languages as well as Java. – Squonk Dec 25 '12 at 20:29

3 Answers3

4

this refers to the instance of the class you are currently coding within.

You cannot use it in a static context because in this situation you are not within any object context. Therefore this doesn't exist.

public class MyClass {

    public void myMethod(){
        this.otherMethod(); // Here you don't need to use 'this' but it shows the concept
    }

    private void otherMethod(){

    }

    public static void myStaticMethod(){
       // here you cant use 'this' as static methods don't have an instance of a class to refer to
    }

}
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • how might you use it for different objects? some examples? -Strings -ints -doubles -chars – AlleyOOP Dec 25 '12 at 20:36
  • @user1923016 it's all the same, any 'instance variable' can use 'this' because it belongs to the instance of this – Blundell Dec 25 '12 at 21:36
2

In android class.this is used to pass context around.

Formal definition of context: It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities. That means if you need to access resources (including R and user interface) you will have to use context.

In java this means the instance of the class that you are in. For example MainActivity.this points to the current instance of the MainActivity. So by using MainActivity.this.foo you are accessing the foo field of MainActivity class.

Hadi Tok
  • 772
  • 8
  • 20
  • 1
    Not just Contexts, **any** Object that was instantiated can use the `this` keyword. Although passing Contexts around is what we *usually* use `this` for. Also your order is off, if you want to specify an object instance based on class name you do `ClassName.this` – A--C Dec 25 '12 at 20:33
1
public class YourClass {

     private int YourInt;

     public setTheInt(int YourInt) {
         this.YourInt = YourInt;
     }
}

"this" is used to see whether an attribute or function belongs to the class we're working on, clearer.

Also, you see that setTheInt operation gets an integer named as the same as your attribute. In that function's namespace, YourInt is not this class's YourInt, but a reflection of the integer coming from setTheInt's calls. "this" helps here to divide the outer and the inner "YourInt"s.

İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64