Probably an easy answer..

0 Hunter Shutt · May 16, 2015
Hey guys,

So I've been watching the Android App Development (Beginners) course, what a mouth full. 

Well anyways, 
I've also watched SOME of the a Java courses as well but I do not know too much about it..
I do know a lot about HTML luckily. And I use JavaScript in HTML and I do know (JavaScript and Java) they're are not alike.
I know JQuery as well and I know that when you use this:

$(document).ready(funnction(){
$('element').click(function(){
$(this).hide();
});
});

That 'this' basically means this exactly:


$(document).ready(funnction(){
$('element').click(function(){
$('element').hide();
});
});

But I'm not to sure that's what it means exactly in Java,

It probably does, but I've researched it, and the way people explain it is getting me confused about it.

And I've just paused Part 21 of the Android App Development (Beginners) to write this topic, and the main reason I wrote all this is because of what Bucky wrote, It's not that it's wrong, it's perfectly right, I just have a habit of wanted to know EVERYTHING there is to know about whats going on.

I want to know everything because I have a better understanding of how this works and how it's being created.
So what bucky wrote was this:


buckysMessage = (TextView)findViewById(R.id.buckysMessage);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);

And again, there is NOTHING wrong with this, I just want a better understanding,
by the way, this was in the class 'onCreate' in the MainActivity.java.
So does this code ^, convert to this?:


buckysMessage = (TextView)findViewById(R.id.buckysMessage);
onCreate.gestureDetector = new GestureDetectorCompat(onCreate,onCreate);
gestureDetector.setOnDoubleTapListener(onCreate);

Thanks in advance!

Post a Reply

Replies

Oldest  Newest  Rating
0 Scott M · May 16, 2015
It's a reference to the current object.

For example, if you had a global variable and a local variable with the same name, you'd use 'this' to access the global variable.

Another example, if you had objects of First and Second, you could give Second access to First's public variables by passing 'this' into Second's constructor.

Take a look at this code using both examples (I assumed you know what objects / instances are):

public class First { // Newly created object of First

private Second second;

public int myVar = 41;

public First() { // Constructor

// Create Second object
second = new Second(this); // this = current instance of 'First'

// 'myVar' now equals 182
System.out.println(myVar);
}
}

public class Second {

private First first; // Global variable

public Second(First first) { // Constructor

first.myVar = 182; // Second can now access all of First's public variables and functions

this.first = first; // Assign global variable to local variable with same name
}
}
0 Hunter Shutt · May 16, 2015
I think I understand it better,

thanks.

So 'this' is using the object 'First' and in the second one it's using the object 'Second'.
Thanks for the feedback!
  • 1

Java / Android Development

107,072 followers
About

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

Links
Moderators
Bucky Roberts Administrator