为什么System.out.println(s1.schoolName);打印的结果是Tsinghua?

class test
{
   String name;
   static String schoolName;



   public static void main(String args[])
   {
    test s1=new test();
      s1.name="张三";
      s1.schoolName="gdgm";


      test s2=new test();
      s2.name="Tom";
      s2.schoolName="Tsinghua";


      System.out.println(s1.name);      
      System.out.println(s1.schoolName);


   }
}


老葱头4265359
浏览 1601回答 2
2回答

ziom

因为你写的schoolName是个类成员,全局变量

慕粉3361607

schoolName是static的,所有的test类实例的schoolName都是相同的;s2.schoolName="Tsinghua";执行后s1.schoolName自然也是Tsinghua
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java