Results 1 to 2 of 2
  1. #1
    CrushingJava is offline Member
    Join Date
    Jun 2013
    Rep Power

    Default Need help to understand the keyword "this" in this code.

    import wheels.users.*;
    import java.awt.Color;
    public class Bolb extends Ellipse {
    private Ellipse _lefteye, _righteye;

    public Bolb() {
    _lefteye = new Ellipse(Color.BLACK);
    _righteye = new Ellipse(Color.BLACK);

    Ok, so I copied this code from a textbook and tried to test it. When I called 'new Bolb()' in the other code, I found out that 'this.setSize(300,300)' makes an Ellipse with size 300*300. However, it confuses me because I thought 'this' refers to the current object. But the current object 'Bolb' consists of three components, the Ellipse(the superclass), and two eyes. How does java know which component 'this' refers to? It is just the way it is that 'this' refers to the super class which is created first?
    Any input will be appreciated!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: Need help to understand the keyword "this" in this code.

    You said it correctly: 'this' refers to the current object 'Bolb'. It can consist of 1000 different parts, but still the object of type 'Bolb' is the one that 'this' points to. super() is a call to the superclass's contructor.

Similar Threads

  1. Replies: 3
    Last Post: 01-26-2013, 03:49 AM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Replies: 5
    Last Post: 04-29-2012, 07:18 AM
  4. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM

Posting Permissions

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