Java中public int和int有什么区别?
我才开始学习 Java(我是一名学生,C++ 是我最了解的基础语言)并且我遇到了访问和修饰符。
在代码中我看到了使用
public int NAME;
// and
int NAME;
它有什么区别吗?如果是这样,有什么区别。它到底有什么作用?
I've only started to learn Java (I'm a student, and C++ is the base langauge I know best) and I've come across access and modifiers.
In code I've seen the use of
public int NAME;
// and
int NAME;
Does it make a difference? If so, what is the difference. What does it do exactly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
public int 是一个没有访问控制的变量。它实际上是一个全局变量。如果您愿意的话,未修改的 int 是“受保护的+” int,它作为受保护的 int 运行,但不能被子类使用。受保护的 int 可以由包含该特定 int 变量的类的子类使用。私有整数只能在该类中使用。通常建议使用尽可能少的公共变量,以促进信息隐藏/封装,从而提高安全性/可靠性。
Oracle 教程 页面很好地介绍了它。
Public int is a variable that has no access control. It is effectively a global variable. Unmodified int is a "protected+" int if you will, it operates as a protected int but can't be used by subclasses. Protected ints can be used by subclasses of the class containing that particular int variable. Private ints can only be used within that class. It's generally advisable to use as few public variables as you can in order to promote information hiding/encapsulation to improve security/reliability.
The Oracle Tutorial page covers it nicely.