//static关键字
//作用:是一个修饰符,修饰成员
//随着类的加载而被加载
//优先对象的存在,被所有的对象所共享,可以直接被类名所调用
class Person{
//实例变量,随着对象的消失而消失
String name;
//静态变量,被static修饰的成员变量只有一份
static String country; //静态变量,类变量,生命周期最长,随着类的消失而消失
//静态方法只能访问静态成员(成员变量,成员方法)
//静态的方法专用不可以定义this super 关键字
static void print(){
String aa="你好,世界";
System.out.println(aa);
}
//非静态方法既能访问静态的成员,又可以访问非静态的成员
void p(){
System.out.println(country);
}
}
public class Main {
public static void main(String[] args) {
Person a=new Person();
a.country="中国";//赋值后不变
Person b=new Person();
System.out.println(Person.country);
System.out.println(b.country);
Person f=new Person();
f.print();
f.p();
}
}
//作用:是一个修饰符,修饰成员
//随着类的加载而被加载
//优先对象的存在,被所有的对象所共享,可以直接被类名所调用
class Person{
//实例变量,随着对象的消失而消失
String name;
//静态变量,被static修饰的成员变量只有一份
static String country; //静态变量,类变量,生命周期最长,随着类的消失而消失
//静态方法只能访问静态成员(成员变量,成员方法)
//静态的方法专用不可以定义this super 关键字
static void print(){
String aa="你好,世界";
System.out.println(aa);
}
//非静态方法既能访问静态的成员,又可以访问非静态的成员
void p(){
System.out.println(country);
}
}
public class Main {
public static void main(String[] args) {
Person a=new Person();
a.country="中国";//赋值后不变
Person b=new Person();
System.out.println(Person.country);
System.out.println(b.country);
Person f=new Person();
f.print();
f.p();
}
}