编辑代码

package test;

public class StaticTest1 {
    private static int staticInt = 2;
    private int random = 2;
//    StaticTest1 static test=new StaticTest1();
    public StaticTest1() {
        staticInt++;
        random++;
    }

    public static void main(String[] args) {
//        StaticTest1 test=new StaticTest1();    	
        System.out.println("类变量与对象变量的值变化");
        StaticTest1 test1 = new StaticTest1();
        System.out.println("  实例1:staticInt:" + test1.staticInt + "----random:" + test1.random);
        StaticTest1 test2 = new StaticTest1();
        System.out.println("  实例2:staticInt:" + test2.staticInt + "----random:" + test2.random);
        System.out.println("静态变量赋值");
        System.out.println("  静态语句块起作用:" + A.staticA);
        A a = new A();
        System.out.println("  构造器起作用:" + a.staticA);
        a.toChange(); 
        System.out.println("  静态方法1起作用:" + A.staticA);
        a.toChange2();
        System.out.println("  静态方法2起作用:" + A.staticA);
        System.out.println("常量赋值");
        System.out.println("  静态语句赋值:" + B.STATIC_B);
    }
}

class A { 
    public static  String  staticA ="A" ;  
    //静态语句块修改值 
    static{  staticA ="A1"; } 
    //构造器修改值
    public A (){  staticA ="A2"; } 
    //静态方法起作用 
    public static void toChange(){  staticA ="A3"; } 
    public static void toChange2(){  staticA ="A4"; }  
}

class B { 
    public static final String STATIC_B ;  // 声明与赋值分离 
    static{STATIC_B="B"; }
}