classTyre{intnumber;Tyre(inti){number=i;System.out.println("thisisatyreofBike"+i);}voidchange(){System.out.println("changeatyre");}}classBike{staticTyret1;staticTyret2;st...
class Tyre{
int number;
Tyre(int i){
number = i;
System.out.println("this is a tyre of Bike"+i);
}
void change(){
System.out.println("change a tyre");
}
}
class Bike{
static Tyre t1;
static Tyre t2;
static{
t1 = new Tyre(1);
t2 = new Tyre(2);
System.out.println("init static variable");
}
Bike(){
System.out.println("this is a bike");
}
}
public class StaticTest {
public static void main(String [] args){
Bike.t1.change();
Bike x = new Bike();
Bike y = new Bike();
}
}
运行结果:
this is a tyre of Bike1
this is a tyre of Bike2
init static variable
change a tyre
this is a bike
this is a bike
展开