你能告诉我在创建 a2 对象后为什么构造函数没有改变值吗

罗希特丁卡尔鬼屋

谁能告诉我在创建 a2 对象后为什么构造函数没有更改值?

public class HelloWorld 
{
    static int x;       // static datamembers
    static int y;       // static datamembers

    HelloWorld()        //constructor 
    {
        x = 9999;
        y = 9999;
    }

    static void display()      // static method
    {
        System.out.println("The value of x is:"+x);
        System.out.println("The value of y is:"+y);
    }

    void clear()
    {
        this.x = 0;      // this pointer
        this.y = 0;      // this pointer
    }

    public static void main(String []args) 
    {
        HelloWorld a1 = new HelloWorld();       // instance of class
        HelloWorld a2 = new HelloWorld();       // instance of class

        a1.display();       // a1 object calls the display
        a1.clear();         // this pointer clears the data
        a1.display();       // cleared data is displayed

        a2.display();       // a2 object calls the display but the values are 0 and 0 why not 9999 and 9999, why didn't the constructor get called?
    }
}
Yousha Arif

由于这一行,a1.clear();您的 clear 方法正在更改静态 x 和 y 变量的原始值。因为如果变量是静态的,每个对象都引用原始变量的单个副本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我没有输出,你能告诉我为什么吗?

你能告诉我这个 Solidity 是如何工作的吗?

你能告诉我为什么我的网站水平滚动吗?

你能告诉我为什么我的命令会出现错误消息无效语法吗

你能告诉我这段javascript代码有什么问题吗?

分数不更新。你能告诉我代码有什么问题吗?

(python)你能告诉我下面代码中有什么问题吗

你能告诉我我的错吗?关于嵌套(Flash CS6 AS3)

你能告诉我如何在Python Web Automation中添加多个代理吗

你能告诉我如何在首页图库的上方和下方保持相等的间距吗?

你能告诉我我用来检索客户上次下订单信息的查询有什么问题吗?

你能修复它或告诉我这有什么问题吗?我想要 Navigacio 鞋面

我正在尝试使用数组删除一行。你能告诉我orm的语法吗?

你能告诉我那个按钮是做什么用的吗?Ubuntu Gnome 16.10-gnome经典

你问我拉而没有告诉我你想与哪个分支合并

我正在处理代码,但没有提供我想要的结果。你能告诉我如何从 fb live 或 post 中抓取 Facebook 评论文本吗?

JS:我有一个测试任务,我不知道怎么做。你能告诉我解决这个任务的选项是什么吗?

你能告诉我为什么我只收到 * 作为输出而不是最初输入的字符串作为解密的字符串吗?

你能告诉我为什么我的将数组转换为数字、加一和返回数组的代码只适用于某些数据集吗?

我的 python 程序是在两个间隔之间找到素数,但我在输出中得到 9,你能告诉我我在做什么错吗

你能告诉我为什么下面的 javascript 代码并不总是在下面的简单 index.html 中出现吗?

找不到“ yo”二进制文件。确保已安装它并在$ PATH中。你能告诉我问题是什么吗?

你能告诉我插入排序的实现是否正确吗?它正在工作,但有些东西感觉可疑

我们可以这样输入数组(a1,a2)中的输入吗?

AtomicInteger a1 在 a2 之前增加,在 a2 之后减少,为什么存在 a2 > a1

有人能告诉我为什么这个程序总是崩溃吗?

有人能告诉我为什么这不起作用吗?

为什么我的对象中的值没有改变?

在Scala中使用match函数时,出现意外结果。有人能告诉我为什么吗?