0

I ran over some java code and i saw something i can't quite get it . Why does the code below work without puting this in front of diameter

class Shape {
    public double area ()
    {
        return 0;

    }
}


class Circle extends Shape {                  
    Circle (double diameter) {                  
        this.diameter = diameter;
    }
    private static final double PI = Math.PI;   
    private double diameter;                    

    public double area () {                     
        double radius = diameter / 2.0;  <-------- LOOK HERE
        return PI * radius * radius;
    }

}

public class Main {
    public static void main(String[] args) {
        Shape s1 = new Circle (2.5);
        System.out.println (s1.area());

    }
}

The code works perfect ... with or without this.diameter/diameter

123onetwothree
  • 636
  • 1
  • 8
  • 17
  • You do not need `this` to access member variables of the same class.It is mostly used in reference to member variables to differentiate between member variables in and function arguments. – Rambler Sep 29 '16 at 11:25
  • Man i use s1 = new ... what if i use s2 = new with different value for the diameter .. how does it know to which object does `diameter` belong to ? – 123onetwothree Sep 29 '16 at 11:28
  • A simple style guide: Use `this` when you are refering to object attributes or the object itself inside the object class. – CloudPotato Sep 29 '16 at 11:31
  • 1
    Small point but don't take a deep copy of `PI`; there is no point in copying constants. – Bathsheba Sep 29 '16 at 11:34
  • ...use `import static java.lang.Math.PI;` instead (following on from @Bathsheba's comment). – Andy Turner Sep 29 '16 at 12:06

1 Answers1

1

In your constructor Circle(double diameter), the function parameter diameter is shadowing the field of the class.

Unless you tell it otherwise (by writing this.diameter), the compiler will assume you are referring to the function parameter not the field. Writing this.foo = foo in Java constructor code is idiomatic.

In your function area(), there is no such ambiguity: this.diameter and diameter both refer to the field.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • i still dont understand, it should have been `this.diameter` enforced . I am talking only about `area() method` . Does java assume that if area() has no diameter parameter then it uses `this` pointer on it by default ? – 123onetwothree Sep 29 '16 at 11:33
  • @123onetwothree Yes. But if you are confused you better use always `this` to make you clear when you use an object attribute and when not. – CloudPotato Sep 29 '16 at 11:38
  • I know that but i was curious why does java dont enforce programmers to put `this` where refering to current object – 123onetwothree Sep 29 '16 at 11:51
  • 1
    Because it would be a pain in the neck to have to write `this.` *every* time you were referring to a field. – Bathsheba Sep 29 '16 at 11:53
  • aaaa, lol :) others languages enforce you too do that – 123onetwothree Sep 29 '16 at 12:04