你理解的不对。static 随着类的加载而加载,类在了static就在了,但其他方法没有加载到内存。所以不能掉其他方法,并不是说主函数有main方法,所以主函数的其他方法也必须是static的,而是因为调用方便,主函数的其他方法也可以是非static的。只需要用该类对象调用即可。
class MainDemo{
public static void main(String[] args){
new MainDemo().show();
}
void show(){}
}
//如果内部类B为静态,则输出语句应为“System.out.println(new A().x+ "," + new B().x + "," + x);”
}
}//内部类
}//外边类
复制代码
作者: ″先森丶玹° 时间: 2013-11-17 00:45
public class Object_Static {
public static void main(String[] args) {
Person p=new Person();
Person d=new Person();
p.name="小李";
d.name="小王";//可以打印看到小李和小王后面都有带美国,这就叫共享
p.show();
System.out.println(Person.country); //直接被类名调用
}
}
class Person2{
String name; //成员变量,实例变量
static String country="美国"; //静态的成员变量,类变量
public void show(){
System.out.println(name+"来自"+country);
}
}