因为你用的是同一个对象A,而且你先执行了A.t1(),然后才执行的A.t2()

解决方案 »

  1.   

    上面的程序能执行吗?在静态方法t1()中能引用外部的非静态变量tt吗?如果tt变量有static进行修饰,那么你的运行结果是没有错的,静态变量为类的变量,不需要经过实例化的。
      

  2.   

    首先我要说的是这样的代码是错误的,在static的方法中是不能使用非static的变量的,,所以tt必须是使用statci的变量。其次,它的生存期是在这个application中,也就是说它相当于C中的全局变量。
      

  3.   

    这样就可以编译了class A
    {
      static String tt = new String();
      public static void t1()
      {
        tt = "test";
      }
      public static void t2()
      {
        System.out.println(tt);
       }
    }
     
    public class B
    {
      public static void main(String args[])
      {
        A.t1();
        A.t2();
      }
     }