Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: Question about why and how Keyword: 'This' works (Example).

  1. #1
    robcardiv is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Question about why and how Keyword: 'This' works (Example).

    public void start() { // Creates - Starts new Thread object
    Thread thread = new Thread(this); // (this) refers to run();
    thread.start();
    }

    public void run() {


    Why does keyword: 'this' refer to run(); ?
    I do not understand how or more importantly WHY keyword:'(This)' refers to the run() method.
    I think I know that 'this' just points to the main variables in a class normally, but I feel like Im missing something important.

    Or another example
    addKeyListener(this);

    I do not really understand.
    I will paste the rest of my short program below in case the information is important.
    Im also not exatly sure why its important.
    I understand quite alot of the basics with classes and constructors and such. but I feel like im missing something important.


    ==

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    public class RecIV extends Applet implements Runnable, KeyListener {

    int x = 400;
    int y = 300;
    int yu = 0;
    int yd = 0;
    int xl = 0;
    int xr = 0;

    int radius = 20;
    private Image i;
    private Graphics gb; // Graphics Buffer, g2 = the buffered image
    double gravity = 25;
    double energyloss = .88;
    double dt = .2;


    public void init() {
    setSize(800, 600);
    setBackground(Color.BLACK);
    addKeyListener(this);
    }

    public void start() { // Creates - Starts new Thread object
    Thread thread = new Thread(this); // (this) refers to run();
    thread.start();
    }

    public void run() {
    while (true) {
    if(x>20){x += xl;}
    if(x<getWidth()-20){x += xr;}
    if(y>20){y += yu;}
    if(y<getHeight()-20){y += yd;}
    repaint();
    try {
    Thread.sleep((long) 16.66); // sleep() in milliseconds 100 =// .1sec. 16.66 is roughly 60fps
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    public void stop() {}
    public void destroy() {}


    public void update(Graphics g) { // gb Double Buffer Method - Copy Paste - Easy transfer
    super.update(g);
    if (i == null) {
    i = createImage(this.getSize().width, this.getSize().height);
    gb = i.getGraphics();
    }
    gb.setColor(getBackground());
    gb.fillRect(0, 0, this.getSize().width, this.getSize().height);
    gb.setColor(getForeground());
    paint(gb);
    g.drawImage(i, 0, 0, this);
    }

    public void paint(Graphics g) {
    g.setColor(Color.GREEN); // x-radius radius*2 moves
    g.fillOval(x - radius, y - radius, radius * 2, radius * 2); // draw point to center of circle
    }

    public void moveUp() {yu=-5;}
    public void moveDown() {yd=5;}
    public void moveLeft() {xl=-5;}
    public void moveRight() {xr=5;}


    public void keyPressed(KeyEvent e) {
    switch(e.getKeyCode()) {
    case KeyEvent.VK_UP:moveUp(); break;
    case KeyEvent.VK_DOWN:moveDown(); break;
    case KeyEvent.VK_LEFT:moveLeft(); break;
    case KeyEvent.VK_RIGHT:moveRight(); break;
    }}

    public void keyReleased(KeyEvent e) {
    switch(e.getKeyCode()) {
    case KeyEvent.VK_UP:if(yu<0){yu=0;}; break;
    case KeyEvent.VK_DOWN:if(yd>0){yd=0;}; break;
    case KeyEvent.VK_LEFT:if(xl==-5){xl=0;}; break;
    case KeyEvent.VK_RIGHT:if(xr==5){xr=0;}; break;
    }}

    public void keyTyped(KeyEvent e) {}

    }

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Question about why and how Keyword: 'This' works (Example).

    'this' basically calls an instance of the current class. The reason 'this' refers to run() in that situation is because the constructor for Thread looks for a Runnable class, and since you implement Runnable, your class qualifies for the Thread constructor. run() is an abstract method of Runnable, and is called on automatically by the Thread when you call thread.start(). For addKeyListener(this), the parameter for addKeyListener() is a keylistener class, and since you implement KeyListener, your class qualifies. So now instead of looking for Runnables abstract methods, it will look for KeyListeners abstract methods.
    Last edited by zFollette; 01-02-2014 at 10:41 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Question about why and how Keyword: 'This' works (Example).

    "This" refers to the current instance of the class in which you use the "this" keyword. It is typically used in:

    1. Constructors when assigning instance fields or calling other constructors.
    2. A short cut for passing in instance of the current class without the need to save a reference. Especially
    when employing listeners.

    Read about it here --> Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Question about why and how Keyword: 'This' works (Example).

    Quote Originally Posted by jim829 View Post
    "This" refers to the current instance of the class in which you use the "this" keyword. It is typically used in:

    1. Constructors when assigning instance fields or calling other constructors.
    2. A short cut for passing in instance of the current class without the need to save a reference. Especially
    when employing listeners.

    Read about it here --> Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    Was I right with my explanation?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Question about why and how Keyword: 'This' works (Example).

    I just wanted to add some additional clarification and point the OP to the tutorials.

    Regards,
    Jim
    zFollette likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Question about why and how Keyword: 'This' works (Example).

    Quote Originally Posted by jim829 View Post
    I just wanted to add some additional clarification and point the OP to the tutorials.

    Regards,
    Jim
    Oh, alright.

Similar Threads

  1. this keyword
    By crazy in forum New To Java
    Replies: 6
    Last Post: 07-07-2013, 05:18 PM
  2. Replies: 5
    Last Post: 05-02-2013, 10:26 PM
  3. The this keyword
    By Army in forum New To Java
    Replies: 15
    Last Post: 12-14-2012, 04:01 AM
  4. Question about how Modulus works
    By calistoknight in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 12:06 PM
  5. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •