java - 为什么所有对象的变量值都会改变

标签 java

我有一个类(class)和年龄字段。但是,当我更改对象的年龄时,该类的所有其他对象也会更改。

private static int age;

谁能帮忙解决这个问题吗?

最佳答案

静态变量类变量,因此,从变量声明中删除static:

private int age;

根据 java 文档:

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.

关于java - 为什么所有对象的变量值都会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19957561/

相关文章:

Java showInputDialog 选择自定义文本

java - 如何使用2个按钮使多线程swing GUI应用程序启动/停止线程

java - 根据 W3C XML 模式定义验证 XSD

java - Spring Security 不允许加载 CSS 或 JS 资源

java - 在 Weblogic Server 8.13 中记录 SQL

java - 为什么这会给我一个边界错误?

java - 什么相当于封装包内部功能的包私有(private)接口(interface)?

java - 在 Apache XMLRPC 环境中获取请求用户的 IP 地址?

java - 使用迭代器时出现 java.util.LinkedList$ListItr.checkForComodification 异常?

java - 使用谷歌地图 V2 在多段线中绘制箭头