还是关于变量的一些知识
static:
1.修饰变量,静态变量存在静态存储区,被多个实例共享,可以用类名直接访问
静态变量是跟随类的加载而加载。生命周期也是跟随类一起创建和消亡
2.可以修饰方法:静态方法可以用类名直接调用,一般工具类中的方法都是静态的,方便使用。
静态方法是跟随类的加载而加载。不能有this关键,当静态函数加载的时this对应实例还没创建。
静态方法不能访问非静态的变量。
*/
class JavaDemo23
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
Student stu1 = new Student();
Student stu2 = new Student();
System.out.println(stu1.country);
System.out.println(stu2.country);
System.out.println(Student.country);
Student.country = "马来西亚";
System.out.println(stu1.country);
System.out.println(stu2.country);
System.out.println(Student.country);
System.out.println(Student.COUNTRY);
Student.test();
}
}
class Student
{
private String name = "香港";
public static String country = "中国";
//java中的常量定义
public final static String COUNTRY = "中国";
public static void test(){
//this.name = name;
System.out.println("test"+country);
}
}