一、类属性、类方法的设计思想

二、使用范围
在Java类中,可用static修饰属性、方法、代码块、内部类。
三、static修饰成员特点
①随着类的加载而加载;
②优先于对象存在;
③修饰的成员,被所有对象所共享;
④访问权限允许时,可不创建对象,直接被类调用
四、类变量
代码:
public class test {
public static void main(String[] args) {
Person.country = "中国";
System.out.println(Person.country);//中国
}
}
class Person{
String name;
int age;
static String country;
public void eat(){
System.out.println("人吃饭");
}
}
内存解析:

五、类方法
说明:
① 随着类的加载而加载,可以通过"类.静态方法"的方式进行调用;
② 静态方法中,只能调用静态的方法或属性;非静态方法中,既可以调用非静态的方法或属性,也可以调用静态的方法或属性
③ 在静态的方法内,不能使用this关键字、super关键字
public class test {
public static void main(String[] args) {
Person.country = "中国";
System.out.println(Person.country);//中国
Person.show();//人吃饭
}
}
class Person{
String name;
int age;
static String country;
public static void eat(){
System.out.println("人吃饭");
}
public static void show(){
eat();
}
}
作者:Java之美
日期:2020-03-29
来源:https://www.cnblogs.com/897463196-a/p/12593226.html