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?