2017-10-14 7 views
-1

아무도 그 이유는 생성자가 값을 변경하지 않았다 a2 개체를 만든 후 말해 주시겠습니까? 이 때문에 라인 a1.clear(); 하여 명확한 방법은 정적 X의 orginal 한 값을 변경하고, Y 변수제발 말해 주시겠습니까 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? 
    } 
} 
+0

프로그램을 실행 했습니까? 0과 0으로 값이 변경되었습니다. 질문과 관련없는 부분은 본문입니까? Java에서 정적에 대해 이해해야합니다. https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html –

+0

어떤 프로그래밍 언어입니까? 사용중인 언어로 질문에 태그를 답니다. 질문을 업데이트하려면 게시물 아래의 ** "[편집]"** 링크를 클릭하십시오. 고맙습니다. – Pang

답변

0

. 변수가 정적 인 경우 모든 객체가 원본 변수의 단일 사본을 참조하기 때문입니다.