Results 1 to 4 of 4
- 12-17-2011, 11:19 AM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
What does "this" refer to in this context?
Hi guys:
Here is my code:
package myPackage;Java Code:package myPackage; public class TestClass { public static void main(String[] args) { FrameClass newFrame = new FrameClass(); newFrame.createFrame(); } }
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
My question is:Java Code:import javax.swing.JButton; import javax.swing.JFrame; public class FrameClass implements ActionListener{ public void createFrame() { JFrame newFrame = new JFrame(); newFrame.setVisible(true); newFrame.setSize(250, 250); newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton myButton = new JButton(); myButton.setText("I am a button!"); newFrame.getContentPane().add(myButton); myButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
myButton.addActionListener(this);
What does (this) do here?
Does it create a new object of Class FrameClass ?
- 12-17-2011, 12:46 PM #2
Re: What does "this" refer to in this context?
"this" is a reference variable that points at the object for the currently executing code.
The "this" on line 13 refers to an instance of the FrameClass.
- 12-18-2011, 12:58 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: What does "this" refer to in this context?
Thanks for the reply.
"this" refers to an instance of the FrameClass. Which istance is it ?
- 12-18-2011, 01:00 AM #4
Similar Threads
-
Yet another "non-static method cannot be referenced fro a static context" problem
By jazzermonty in forum New To JavaReplies: 4Last Post: 03-14-2011, 12:05 AM -
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks