package test;
public class StaticTest1 {
private static int staticInt = 2;
private int random = 2;
public StaticTest1() {
staticInt++;
random++;
}
public static void main(String[] args) {
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"; }
}