static变量的特点

class Test{
    static String name;
    int num;
    
    public void print(){
        System.out.println(name+num);
    }
}


public class Homework{
     
    public static void main(String[] args){
      Test per=new Test();
      Test per2=new  Test();
      per.name="abc";
      per.num=123;
      per.print();
      per2.name="def";
      per2.num=456;
      per2.print();
       Test.name="songdian";
       per2.print();

        per.print();

}

}

在修改了static变量后per和per2调用的name属性全部变成了songdian

  

你可能感兴趣的:(static变量的特点)