|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
"this" is my new problem
i am having a hard time understanding what "this" does.
// Listen to events from Convert button. convertTemp.addActionListener(this); i've been able to ignore it for a while, but i need to be able to know what it does now, the java book i have does not help this is the entire porgram if you want to look at it http://java.sun.com/docs/books/tuto...sConverter.java |
|
#2
|
||||
|
||||
|
"this" generally refers to the class that you're calling it from
an easy way to understand it is the fact that you can call instance variables by using this Code:
public class Hey {
String whatever;
// Constructor
public Hey() {
this.whatever = "Hey you!";
}
}
I'll think of a better way to explain this if you need it... =) |
|
#3
|
|||
|
|||
|
Ah, so it can be used to shorten the typing needed for calling things inside a class, and maby reduce bugs that may result if you change the class name. that clears up a lot. I was thinking of something very different.
I guess my next problem to look at is to learn how actionlistener works. After that I may be able to figure out what "this" does in the program i'm looking at. Thanks for the help, and you'll probably hear more from me, because i'm a T.A. in a computer class, and I have to help the teacher try figure out what and how to teach the kids, which involves knowing java better myself -_- |
|
#4
|
||||
|
||||
|
int main() {
button.addactionlistener(this); } int buttonclicked() { //do stuff } Although this is not the corect syntax, it illustrates the idea. The addactionlistener(this) tells the actioncapable class (such as a button) which class that contorls actions happening to it (such as someone clicking on the button). Hope that explains it. (Look at sun's java api for an example with correct syntax that you can copy/paste) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > "this" is my new problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|