Java-全局变量与全局方法 [英] Java - Global variables vs Global methods

查看:65
本文介绍了Java-全局变量与全局方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在研究一个5000多个班级的项目,然后在每个班级中都看到了这一点:

I was looking into a project whit 5000+ classes, then I see this in each class:

private static int foo = 1;

private static void setfoo(int value)
{
     foo = value;
}

private static int getfoo()
{
     return foo;
}

所以,我想为什么不使用全局变量并设置它(如果无论如何都是静态的).

so, I think why dont use a global variable and set it if anyway is static.

全局方法和变量之间存在资源使用差异吗?

There is a resource use difference between global methods and variables?

谢谢.

推荐答案

如果将来想添加验证(或添加日志记录,或进行访问统计或...),则字母和设置器很有用.

Getters and Setters are useful, if you e.g want to add validation in the future (or add logging, or make access statistics or ...)

这篇关于Java-全局变量与全局方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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