public class staticvariable 
{
static String color = "绿色";
public staticvariable(String color)
{
this.color += color;
}
public static void main(String args[])
{
staticvariable sv1 = new staticvariable("黄色");
staticvariable sv2 = new staticvariable("红色");
System.out.println(sv1.color);
System.out.println(sv2.color);
System.out.println(color);
}
}
输出结果为:
绿色黄色红色
绿色黄色红色
绿色黄色红色
若把static String color = "绿色";改为 String color = "绿色";再把System.out.println(color);注释掉。
输出结果为:
绿色黄色
绿色红色
请问这是为什么呢?

解决方案 »

  1.   

    这是对于java中初始化问题。static初始化块实在构造函数之前完成的。并且static类型变量只实例化一次。你创造了两个实例,对于非static成员变量是不同的,所以会出现你看到的情况。
      

  2.   

    static 会共享变量的值,
      

  3.   

    静态变量 是随类而存在的,而非对象;
    执行staticvariable sv1 = new staticvariable("黄色");之后,this.color = “绿色黄色”;
    执行staticvariable sv2 = new staticvariable("红色");之后,this.color = “绿色黄色红色”;
      所以,接下来的输出都是“绿色黄色红色”;
    而,非静态变量 是随对象而存在的;
      

  4.   

    static 变量只实例化一次,所有对象共享一个Static String color
    而this.color+=color相当于字符串相加,字符串相加是接在原字符串后的
    你实例化两次,分别为“黄色“,“红色” 
    所以最后结果连续输出了三次:绿色黄色红色相信现在你也知道去掉static之后结果变化的原因了。
      

  5.   

    我的理解 不知道理解的对不
    public class staticvariable 
    {
    static String color = "绿色";  //当JVM加载的时候就会被第一个初始化  
    public staticvariable(String color)
    {
    this.color += color;
    }
    public static void main(String args[])
    {
    staticvariable sv1 = new staticvariable("黄色");  //1 . 此时创建了一个实例 此时 color  =  绿色黄色
    staticvariable sv2 = new staticvariable("红色");  // 2. 此时有创建了一个实例 此时 color  =  绿色黄色红色
    //因为你在创建实例的时候就已经把值赋值完毕   static 只会被加载一次  
    //所以你以下输出都是你所创建最后一个实例的值  也就是绿色黄色红色
    System.out.println(sv1.color); 
    System.out.println(sv2.color);
    System.out.println(color);
    }
    }大神们 如果小弟说错了  求狂批......求教育... 如果没错 也求大神在更进一步的指点....
      

  6.   

    对象实例化之前会先初始化静态变量 而且 无论创建多少个对象  静态变量都只有一份 供其它对象共享 所以  在sv1 sv2 new出来之前 color为绿色 new出来sv1之后为由于字符串连接的效果 color为绿色黄色 new出sv2之后为绿色黄色红色   而在打印的时候 访问的都是同一个color 所以都是绿色黄色红色  而如果是实例变量 只有在对象实例化之后才开始被初始化 而且 它不被共享 所以在sv1访问到的只是sv1的color 为绿色黄色  sv2访问到的是sv2的color 为绿色红色  不知明白否????
      

  7.   

    执行staticvariable sv1 = new staticvariable("黄色");之后,this.color = “绿色黄色”;
    我要是只打印
    System.out.println(sv1.color);那他就不应该再输出“红色”了吧,可是结果还是输出“绿色黄色红色”,为什么呀,我没让他输出“红色”啊;这是为什呢
      

  8.   

    public class staticvariable 
    {
    static String color = "绿色";  //当JVM加载的时候就会被第一个初始化  
    public staticvariable(String color)
    {
    this.color += color;
    }
    public static void main(String args[])
    {
    staticvariable sv1 = new staticvariable("黄色");  //1 . 此时创建了一个实例 此时 color  =  绿色黄色
    System.out.println(color); //你放在这里执行 此时会输出绿色黄色}
    }
      

  9.   

    你应该搞清楚代码的执行顺序
       当编译的时候首先static会被放到静态域中
       static String color = "绿色";
       
       当你去创建这个类的实例时候staticvariable sv1 = new staticvariable("黄色"); 
       你会调用他的构造方法
       public staticvariable(String color)
        {
      this.color += color;  
       }
      最后你输出的时候结果肯定会变的    整理下大概过程是
          //加载静态属性
         1.  static String color = "绿色";     //创建实例
         2.  staticvariable sv1 = new staticvariable("黄色");
         //调用有参构造方法
         3.  public staticvariable(String color)
         //给color 追求内容
         4.this.color += color;  
         //color此时等于绿色黄色
         5.JVM给color追求内容  追加后的结果是  绿色黄色
      

  10.   

    你源代码中的color定义的static变量,staticvariable类构造方法中“this.color += color;”等于把两个字符串相加赋值给了static color,该static color 静态变量为所有staticvariable对象共享。所以就出现了这样的结果