-1

I'm currently learning Java, and one of the examples in the textbook is as follows:

class Circle{
    private double radius;
    private double area;


    public void setRadius(double r){
        radius = r;
        setArea(Math.PI * radius * radius);
    }
    public void setArea(double a){
        area = a;
    }

Looking at the setRadius method, is there a style preference/difference between writing what's above VS writing the following?

    public void setRadius(double r){
        radius = r;
        setArea(Math.PI*r*r);
    }

One of my classmates is making the argument that one should use the parameter r instead of the private variable radius because a public method shouldn't have direct access to a private variable unless it's by using a getter. Is this more of a style preference or is he right in that public methods directly accessing private methods is bad?

Tyler
  • 365
  • 2
  • 4
  • 15
  • 2
    It depends on requirement...if you see in Circle class there is instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable –  Nov 13 '18 at 03:54
  • 6
    In `setArea` you have a **bug**; it should be `this.area = a;` **not** `area = area;` – Elliott Frisch Nov 13 '18 at 03:55
  • @ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it. – Tyler Nov 13 '18 at 03:56
  • 5
    *because a public method shouldn't have direct access to a private variable* I **disagree**. – Elliott Frisch Nov 13 '18 at 03:58
  • 2
    Your classmate is wrong. Public methods access private fields all the time. – Dawood ibn Kareem Nov 13 '18 at 03:59
  • It depends what is your code requirement, if you want to initialize Circle variable then you should follow this.radius=r and also this.area=a as well also i disagree with your classmate statement.you can access private fields from public methods any time all pojo class is an example for that. – GauravRai1512 Nov 13 '18 at 04:04

2 Answers2

4

That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius.

IMHO, the area in the class should be calculated, using the radius, once requested, instead of storing both the radius and the area. This is how the program should look:

class Circle {
    private double radius;

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea(){
        return Math.PI * radius * radius;
    }
}

As you see above, the r vs radius question goes away completely.

mjuarez
  • 16,372
  • 11
  • 56
  • 73
  • 1
    In as far as the current question goes, I agree with you; however, the class should (arguably) override `Object#equals(Object)`, `hashCode()` and `toString()`. There might also be some value to making the `radius` immutable and using the constructor to set it. – Elliott Frisch Nov 13 '18 at 04:50
1

In this case both method call are correct

setArea(Math.PI * radius * radius);

and

setArea(Math.PI*r*r);

public method shouldn't have direct access to a private variable unless it's by using a getter

I don't agree for this. because getter also a public method and no meaning of this sentence.

janith1024
  • 1,042
  • 3
  • 12
  • 25
  • 1
    *getter is also a public method* but have a look at [this answer](https://stackoverflow.com/a/1568230/1039555) to see why it still is a good practice – Kartik Nov 13 '18 at 04:06