java:使用最终的静态int = 1比仅仅普通1更好吗? [英] java: is using a final static int = 1 better than just a normal 1?

查看:82
本文介绍了java:使用最终的静态int = 1比仅仅普通1更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚看到一个类(一个很大的API模块),其中有很多东西,比如

I just saw a class (an big API module), where there is alot of stuff like

readParamString(Object params, int index)

他们在该班级有10个字段

and they have in that class 10 fields

final static int PARAM1 = 1
...
final static int PARAM10 = 10

与readParam函数一起使用

which are being used with readParam functions

但你也可以使用普通的int,如 readParamString(o,1);

but you can also use a normal int like readParamString(o, 1);

最终的静态int比使用普通的int更好吗?

is final static int somehow better than using a normal int ?

推荐答案

通过声明final static int,你将对int变量有一个引用。另一个更重要的是你不能改变int的值一旦宣布决赛。
如果int不是final,那么你可以改变变量的值。
因此,如果您不想通过输出代码更改变量值,那么您可以使用final static int。

By declaring final static int you will have a single reference to the int variable.And one more important thing is you can not change the value of int once declared final. If int is not final then you can change the value of variable. So if you don't want to change change the variable value through out code then you can use final static int.

这篇关于java:使用最终的静态int = 1比仅仅普通1更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆