Constructor: final double in Java

I can't understand why the double XX and YY are placed last in the constructor Why don't I double them XX and double YY Why?

public class Point {
private final double x,y;
private double distance; 

 public Point(final double xx,final double yy) {
  this.x = xx;
  this.y = yy;
  this.distance = -1; 
 }

}

Solution

These parameters need not be final

There are two reasons for the parameters:

>Use them in the inner classes declared in this function; > Prevent accidental changes to its value

Obviously (1) is not applicable

(2) It's not necessary because it's such simple code that you can see that it doesn't change parameters

There is a school of thought that all parameters and local variables should be declared final, because it is easier to reason about code, just as it is easier to reason about code with immutable types

There is another school of thought that adding the final result anywhere is just unnecessary noise, and if you are writing a method that cannot judge whether the value is changing, your method is too long

To a large extent, the parameters and local variables are ultimately attributed to individual / team preferences

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>