Im really new to android development and coding and I have this quesion

0 Mert AKUZUM · September 19, 2015
We oftenly use something called "this". I really dont understant what is that and why we use it. 
for example, when we code 
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
why we use the "this" stuff? can someone explain with detail.

I also dont understand why sometimes we do
public void
private
public static

Post a Reply

Replies

Oldest  Newest  Rating
0 Narjis Hasan · September 21, 2015
This is a pointer. Think of it as if you are pointing your finger towards something and saying 'this'. For example, you have a whole lot of shelves, full of books. And you point over to one and say 'Today I'm gonna read the book ABC from this shelf'
So when you are saying 
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);

you are picking the current object and saying that the gestureDetector in this (current) object is equal to a GestureDetectorCompat (you're creating a GestureDetectorCompat - a new object) using the attributes(qualities), passing them as parameters. Two parameters are required here and they are both from this object.

This is not a very technical explanation, but hope it helps you in understanding "this".
0 SHADE SPIRIT · September 21, 2015
Hi @Mert_AKUZUM :D


"this" keyword is ofently used in java and has many functionalities, but the three most functionalities you would be interrested in are:

1)  refer to a variable: example public void method(int x, double y){ y=0; this.x = x }
//"this.x" is a variable, whereas "x" is a parameter.
//while x,y are also declared as variables in the class that defines "method".

2) refer to a class (object): example this.object //means that we are refering to "that" object.

3) using "this" as a paramater: example public class Alpha { //stuff    method(this); //refering to class Alpha instance as paramater.

PS: you can also use it with overloaded constructors, but i hope you won't be needing it :P
      Also, my personal advice is to search here for further explanation :
https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html                     
this site is helpful you'll find all what you need concerning the Java programming language.
0 Gary Whitney · September 24, 2015
Hi Narjis
I will explain the second part of your question, since no one else did.
Objects can have variables and methods. When you declare that you want them to exist you have to think about their visibility and  where they are going to reside.
Public means they are visible outside the place where you declared them.
Private means they are not.
If you declare variables at the top of your class then all members of the class can see them. If you declare them inside a method then only the method can see them.

Methods can also be public or private. Methods that return something must declare the type of thing they are returning. If a method isn't going to return anything then you use the keyword VOID.

Static is a little harder to explain.
Objects are created from a blueprint called a Class. Variables and methods are created as part of the object unless you  use the keyword STATIC. A static variable stays defined in the Class but is not created as part of the object.

Can you see why the main method is Public and Static and Void?
Public so it can be passed arguments when it is called.
Static because you don't want an every object of the class to get this method.
Void because it doesn't return anything.

The classic use of a static variable is to store a value that you want all objects made from the Class to have access to.

Bonus Information.
New programmers get stumped by the error that the static method (main) can not access a non static variable or method.
This is why you frequently see an the main method calling the class constructor method i.e. the method with the same name as the class.
Another way around the static dilemma is to make another class in your package and then have that class do what you want to do by having main call a method in the other class.
Say I have another class called ScanDemo, that demonstrates a java concept like using the Scanner to get user input and perform  calculation. If I put a public method called go() in ScanDemo I can:
1. Make an object of that class from within main. ScanDemo ObjectName = new ScanDemo;
2. Then call the go() method of the class and have it do everything over there. ObjectName.go();

Hope this answers the other question.
  • 1

Java / Android Development

106,937 followers
About

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

Links
Moderators
Bucky Roberts Administrator