Results 1 to 6 of 6
  1. #1
    Hodge is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default "this" keyword queston

    I am trying to understand conceptually how the "this" reference works. When I run the following code, I get 0:

    public class Circle {
    private double radius;

    public Circle(double radius) {
    radius = radius;
    }

    public double getR(){
    return radius;
    }
    public static void main(String[] args){
    Circle c = new Circle(5.0);
    System.out.println(c.getR());
    }
    }

    When I add "this" to the constructor I get 5.0:


    public class Circle {
    private double radius;

    public Circle(double radius) {
    this.radius = radius;

    }

    public double getR(){
    return radius;
    }
    public static void main(String[] args){
    Circle c = new Circle(5.0);
    System.out.println(c.getR());
    }
    }

    If I change the parameter name to r instead of radius in the constructor I get 5.0 again:

    public class Circle {
    private double radius;

    public Circle(double r) {
    radius = r;

    }

    public double getR(){
    return radius;
    }
    public static void main(String[] args){
    Circle c = new Circle(5.0);
    System.out.println(c.getR());
    }
    }

    Why is this? (no pun intended....)

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,084
    Rep Power
    4

    Default Re: "this" keyword queston

    You are assigning the value of the parameter to itself. The field remains 0.
    Java Code:
    public Circle(double radius) {
       radius = radius;
    }
    If you add "this.", the compiler knows you're talking about the field radius, instead of the parameter. If you rename the parameter, then the compiler knows that radius is the field. Make it a habit to always add it. Then you'll never get surprises like this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: "this" keyword queston

    "this" refers to the instance of the enclosing class of where it is used.

    Assume you have a class named Foo with an instance field val.

    Within the class Foo you can do:

    Foo foo = this;

    Then foo.val and this.val reference the same field.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Hodge is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: "this" keyword queston

    Thanks - I think I understand what is happening now. If I change the default radius value to 3 and run the following code i get 3.0. If I understand correctly, the default value of radius is being used. The parameter value appears to be hidden or not accessable unless "this" is used or a different parameter name is used. Am I correct in this assumption?

    public class Circle {
    private double radius = 3;

    public Circle(double radius) {
    radius = radius;
    }

    public double getR(){
    return radius;
    }
    public static void main(String[] args){
    Circle c = new Circle(5.0);
    System.out.println(c.getR());
    }
    }

  5. #5
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,084
    Rep Power
    4

    Default Re: "this" keyword queston

    The value will always be 3, no matter what parameter you pass. Effectively, your code is broken. This will never work. This is something you don't want to see in your code. Either rename the parameter, or prefix with "this.". The latter is my personal choice.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: "this" keyword queston

    Quote Originally Posted by Hodge View Post
    The parameter value appears to be hidden or not accessable unless "this" is used or a different parameter name is used. Am I correct in this assumption?
    It sounds like you get it. To put it another way, when a variable has the same name as a variable with a wider scope, the variable with the narrower scope hides the variable with the wider scope. If you refer to that name without qualification, you refer to the variable with the narrower scope. To access the variable with wider scope, you have to qualify its name with "this". Sometimes when you have nested classes, it's even necessary to specify which "this" you're referring to, e.g., Circle.this.radius. (But when that happens, maybe it's time to think about changing your variable names so they don't hide each other.)
    Get in the habit of using standard Java naming conventions!

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, 08:31 PM
  2. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  3. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  4. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08: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
  •