java学习修饰符之static

阅读 113

2022-03-11

static关键字是静态的意思,可以修饰成员方法,成员变量。

static修饰的特点

被类的所有对象共享

    这也是我们判断是否使用静态关键字的条件

可以通过类名调用

    当然也可以通过对象名调用 

     推荐使用类名调用

package com.ithema_33;
/*
测试类
*/
public class StaticDemo {
public static void main(String[] args) {
Student.university = "传智大学";

Student s1 = new Student();
s1.name = "林青霞";
s1.age = 30;
// s1.university = "传智大学";
s1.show();

Student s2 = new Student();
s2.name = "风清扬";
s2.age = 33;
// s2.university = "传智大学";
s2.show();


}
}
package com.ithema_33;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

public class Student {

public String name;//姓名
public int age;//年龄
// public String university;//学校
public static String university;//学校

public void show(){
System.out.println(name + "," + age + "," + university);
}

}

精彩评论(0)

0 0 举报