Can't understand why 'this.gestureDetector' is used

+3 Anuroop Gambhir · March 11, 2015
From the code from tutorial 21 and 22 



private TextView changingTitle;
    private GestureDetectorCompat gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        changingTitle = (TextView) findViewById(R.id.changingTitle);
        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);
    }


i cant understand 
this.gestureDetector = new GestureDetectorCompat(this, this);


what does "this" mean ??
and why this.gestureDetector not just gestureDetector ?

Post a Reply

Replies

Oldest  Newest  Rating
0 Nicholas Eason · March 9, 2016
You can exclude the this keyword in your specific case and nothing bad will happen, so there's no particular reason for this.gestureDetector over gestureDetector.
0 Ninad Bagwe · March 8, 2016
It's like you are in a method and method in java can have it's own variable called gestureDetector 
of the same class GestureDetectorCompat so in order to distinguish the variables of the class class with the class with the variable of the method we use this which tells us the variable is of the class and not of the method 
+1 Jake Scaife · March 12, 2015
'this' refers to the current instance of the class you are in.

technically you could use just gestureDetector and chances are your IDE will forgive you.

However if you are using an instance variable, you need to reference it using this.

its the same as doing object.doSomething() where 'this' is the object you are currently referencing - kinda like a variable object.

For example if you have a class called Dog and you create an instance of dog called Fido, then inside the Dog class you have a line which says this.name = "fido"; it will set the name of that particular object you created. If you create a different dog and call it Killer, this would refer to the Killer object when you reference it.
  • 1

Java / Android Development

107,234 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator