【发布时间】:2013-02-01 17:50:54
【问题描述】:
可能重复:
Java final modifier
这只是一个关于偏好的小问题。 final修饰符的正确用法是什么?
我注意到,从源到源,我比其他人更倾向于使用它。我把它用于方法的参数、方法中的变量以及我所能做的。这是必要的还是我做得过火了?
参考:
private final int x;
private final int y;
private final int id;
private final int width;
private final int height;
public Widget(final int id, final int x, final int y, final int width, final int height) {
this.id = id;
this.x = x;
this.y = y;
this.xmod = x;
this.ymod = y;
this.width = width;
this.height = height;
}
【问题讨论】:
-
你能分享一些例子吗?
-
我认为您应该仅在必要时使用
final。像任何东西一样,你不应该滥用它。