Take the 2-minute tour ×
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;

import javax.swing.*;
import java.awt.event.*;

public class CursorClass implements ActionListener{
        JButton btn;
        JFrame frame;

    public static void main(String[] args) {
        CursorClass cc = new CursorClass();

    public void launchButton(){
        frame = new JFrame("Main Window");
        frame.setSize(400, 400);

        btn = new JButton("Close Main Window");

        btn.addActionListener(this); // <-------- 

        JPanel pnl = new JPanel();


    public void actionPerformed(ActionEvent event){

As you can see in the launchButton method.. the btn.addActionListener(this) function..

what is the this? refering to?

Is it refering to the whole CursorClass? I'm just a tad bit confused here. I can't understand what I passed to the button object. I know I added the ActionListener to it.. and now the button (which is the event source) will respond to the button when it is clicked but adding this in that method.. I'm unable to see what this is .. I know that the most common answer is this refers to current object, etc.. But I need someting more specific.. is my class that I wrote this in considered to be this?

share|improve this question
The instance of the btn at THE exact moment. –  Tdorno May 10 '13 at 21:54
Yes. The this reference points to the instance owning the surrounding scope. In this case, since your class implements ActionListener, you are providing it as a parameter to the addActionListener method –  Ron Dahlgren May 10 '13 at 21:55
this refers to the current instance of CursorClass which launchButton was called on. –  Travis J May 10 '13 at 22:11
Also, have a look at the typical setTer, where you end with something like this.x=x –  Thorbjørn Ravn Andersen May 10 '13 at 22:31

5 Answers 5

up vote 2 down vote accepted

this, as is being executed in your code, refers exactly to the instance cc created there:

CursorClass cc = new CursorClass();

After that instantiation, you call:


So, in your example, this is cc.

The method signature for addActionListener is public void addActionListener(ActionListener l), so you have to pass an instance of ActionListener, and as you can see in you class definition:

public class CursorClass implements ActionListener{

CursorClass is a valid class since it implements ActionListener.

What you can see here is the observer pattern, where the notification method is the implemented method public void actionPerformed(ActionEvent e){...}

share|improve this answer
By far the BEST explenation . thank you kindly for dumbing it down like that. :) –  Heartbound07 May 11 '13 at 18:40
My pleasure :-D –  Alfonso Nishikawa May 13 '13 at 6:32

The simplest answer is that it's referring to the object of this class, or a class that extends this class, that called the method (launchButton()).

share|improve this answer

Object of your class is considered to be this.

Lets say you have two objects of CursorClass, A & B.

So, this in object A will refer to object A & this in object B will refer to object B.

share|improve this answer
Also called self in some other languages. I think self is more intuitive, maybe it helps to understand the Object metaphor. –  jpaugh May 10 '13 at 21:58
yeah....it helps to understand the Object metaphor –  Vishal Pawale May 10 '13 at 22:04
Thank you guys for responding so quick. I will accept the answer later today, I have to go but thank you, that helped. –  Heartbound07 May 10 '13 at 22:06
@Draconian If you're new to "Object oriented programming", or if you don't know what that is, you should do a search for it and read up about it. (Maybe on Wikipedia). Java is all about OO, so you'll have to get to know it pretty well. –  jpaugh May 10 '13 at 22:10

Yes, this in java and other Object-Oriented languages is referring to the current class.

So in your case this == CursorClass.

share|improve this answer

Your CursorClass implements ActionListener. this refers to the current instance of CursorClass. Essentially, the code adds itself ("this" object) as an action listener.

The reason it refers to an instance of CursorClass is because it's being called from launchButton, which is a method in CursorClass.

share|improve this answer
+1, By 3 seconds, well done WChargin :) –  Jason Sperske May 10 '13 at 21:56

Your Answer


