Class 你能告诉我,在创建a2对象之后,为什么构造函数没有更改值
有谁能告诉我,在创建a2对象之后,构造函数为什么不更改值Class 你能告诉我,在创建a2对象之后,为什么构造函数没有更改值,class,object,constructor,static,static-methods,Class,Object,Constructor,Static,Static Methods,有谁能告诉我,在创建a2对象之后,构造函数为什么不更改值 public class HelloWorld { static int x; // static datamembers static int y; // static datamembers HelloWorld() //constructor { x = 9999; y = 9999; } static vo
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?
}
}
因为这条线
a1.clear()代码>
清除方法是更改静态x和y变量的原始值。因为如果变量是静态的,则每个对象都引用原始变量的一个副本。您是否运行了程序,它将值更改为0和0,您的问题与它的主体无关?您需要了解java中的静态语言这是什么编程语言?请在你的问题上加上使用的语言。要更新您的问题,请单击帖子下的“”链接。非常感谢。