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

    Default what does the keyword "this" mean here?

    Here are my codes:
    public class MyApp {
    private Robot _robot;

    public MyApp() {
    ...
    _robot = new Robot (100, 200);
    }
    }
    ------------------------------
    public class Robot {
    //instance variables
    public Robot (in x, in y) {
    _something1 = new Something ();
    _something2 = new Something ();
    this.setLocation(x, y);
    }
    public void setLocation(int x, int y) {
    ...
    }
    }

    In the codes above, constructor of MyApp creates an instance _robot of the class Robot and calls for the constructor of Robot. Then here's my confusion, I know "this.setLocation(x, y)" invokes the method of its own. However, when I changed "this." to "_robot.", an error "cannot find symbol - variable _robot'' came out. Didn't "this." and "_robot" mean the same thing? I thought "this." refers to the current object, which is _robot? Clear my confusion plz! Thank you!

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

    Default Re: what does the keyword "this" mean here?

    Consider the following example (found here):
    Java Code:
    public class Point {
        public int x = 0;
        public int y = 0;
            
        //constructor
        public Point(int x, int y) {
            x = x;
            y = y;
        }
    }
    Look at line 7 and 8. What happens there? What is the value of field x and y? If I call that constructor with new Point(3,4), will they be 3 and 4? No. Because you assing the parameter to itself. That's why you need to tell the contructor where to find it. The 'this' keyword will point to the object that is being instantiated in this case. The correct code would be:
    Java Code:
    public class Point {
        public int x = 0;
        public int y = 0;
            
        //constructor
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    Consider this code:
    Java Code:
     _robot1 = new Robot (100, 200);
     _robot2 = new Robot (200, 300);
     _robot3 = new Robot (300, 400);
     _robot4 = new Robot (400, 500);
    Every robot is an instance of class Robot. Inside the Robot class, where does 'this' refer to? 'this' points to the instance. So for _robot1, 'this' points to _robot1, for _robot2, 'this' points to _robot2, etc. You can't rip out 'this' and replace it with the variable name. That doesn't make sense.

  3. #3
    CrushingJava is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: what does the keyword "this" mean here?

    But in my codes, does "this" refers to _robot, because I think _robot is the instance being created, which is what "this" mean?
    And for example,


    public class Something {
    private Ellipse _sun;

    public Something() {
    _sun = new Ellipse();
    _sun.Setlocation(100,100);
    }
    ...
    }
    Using the variable name works in this case. What's the difference? Thank you. I am very new to Java :(

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,384
    Blog Entries
    7
    Rep Power
    20

    Default Re: what does the keyword "this" mean here?

    In your latest example _sun refers (or 'points to') an Ellipse object and so does 'this'; it points to the object on which a method is invoked so the method can use 'this'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: what does the keyword "this" mean here?

    In the example you gave there is no difference. Using "this" would not make a difference. But there may be times when you need to disambiguate between identically named variables (as SurfMan demonstrated).

    Here is another (very contrived) example:

    Java Code:
    public class Example {
       int value = 5;
       public static void main(String[] args) {
         Example ex = new Example();
         ex.TwoValues(20);
       }
      // example only - not good design technique 
       public void TwoValues(int value) {
          System.out.println(value); // Prints 20
          System.out.println(this.value); // Prints 5
       }
    }
    For additional info check out 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.

Similar Threads

  1. Need help to understand the keyword "this" in this code.
    By CrushingJava in forum New To Java
    Replies: 1
    Last Post: 06-08-2013, 07:31 PM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •