Please explain “this” to me

前端 未结 6 1334
轮回少年
轮回少年 2021-01-01 05:03

I\'ve read hundreds of explanations on \"this\" in java and I\'m really having trouble grasping it. I\'m learning android and java side-by-side, I know it\'s harder that wa

相关标签:
6条回答
  • 2021-01-01 05:11

    Instance methods (those not declared static) of a class can only be executed by reference to some instance of the class. For example:

    class Foo {
        public void doSomething() {
            // "this" refers to the current object
            . . .
        }
        . . .
    }
    
    // then later:
    Foo aFoo = new Foo();
    aFoo.doSomething(); // "this" will be equal to "aFoo" for this call
    
    // The following is illegal:
    doSomething();
    
    // so is this:
    Foo.doSomething();
    

    Inside the method doSomething(), the variable this refers to the specific instance of Foo that was used to invoke the method (in this example, the current object referenced by aFoo).

    0 讨论(0)
  • 2021-01-01 05:14

    Ok I'll see how I go :P

    Think of a Java object (class) as an individual entity, which has certain things which define what it IS (properties) and certain things it can DO (methods)

    For example, take a (very abstract) class named Machine

    class Machine {
        Piston piston1;
        ArrayList<Gear> gears;
    
        public void addSomeNewGears(ArrayList<Gear> gears)
        {
            for(int i = 0; i < gears.size(); i++)
            {
                this.gears.Add(gears[i]);
            }    
        }
    }
    

    In the Method addSomeNewGears we actually have access to TWO Lists named gears:

    • The Machine object's current gears,
    • The new ones we want to add.

    Because they are both called gears it can be ambiguous as to which one we want to access, however the new List will take priority as it is declared locally in the method.

    To access the Machine's gears, we need to use the this keyword, which tells the compiler we are looking for the class's gears, not the method's

    Hope this helps!

    0 讨论(0)
  • 2021-01-01 05:18

    this refers to the current Object's reference.

    Read this for more understanding.

    To give an example from the link:

    public class Point {
        public int x = 0;
        public int y = 0;
    
        //constructor
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    

    Here, to differentiate from the x of the Point and x of the argument, you need to tell the compiler the difference. You achieve that using this. Meaning, when I write, this.x it means, the particular x belongs to the current Object, which in the case is Point.

    Taking example from the code that you have provided:

    AlertDialog.Builder(this)
    

    AlertDialog.Builder() takes in a Context as a parameter in its constructor. But here, you don't do Context someContext = new Context(); and pass that as the parameter, because you simply need to pass your current Activity's Context. So you simply use this.

    0 讨论(0)
  • 2021-01-01 05:25

    The keyword this, like others have said, is just a reference to the current object. This is usually implicit, such that if you have a class as so:

    class ThisExample{
        int x;
        public ThisExample(int x){
            this.x = x;
            someMethod();
            this.someMethod();
        }
    
        void someMethod()
        {
        ...
        }
    }
    

    Using this.x = x helps to differentiate between the member variable owned by the class and the variable being passed into the constructor. Also, calling this.someMethod() and someMethod() does exactly the same thing because the this is implied.

    In Android, sometimes you will see a method with this being passed in like someMethod(this). What happens here is that this is referring to the current Activity's Context, which is just a bunch of information explaining everything about the Activity.

    0 讨论(0)
  • 2021-01-01 05:27

    Think of this as "itself". If you pass this to a method, you're simply passing an instance of the object to the method.

    ie: Student is an object, as is Classroom. If I want to add a Student to the Classroom, I might tell Student to add itself to the classroom (classrooms can't find students, can they?). So, I will say student.addToClassroom(new Classroom(), this);

    0 讨论(0)
  • 2021-01-01 05:35

    this is none other than the reference of current object. this will be very useful to identify that the members belongs to the current class. For example, Class Sample{ int a; Sample(int a){ this.a=a; } } "this" will differentiate current class variable and other variable

    0 讨论(0)
提交回复
热议问题