一.Static关键字:代表静态的,可用于修饰 属性,方法,代码块,内部类
1.static修饰的属性(静态变量或类变量)
①随着类的加载而加载,随着类的消失而消失。(生命周期最长)
②static 修饰的属性被该类所有的对象所共享
③一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
④静态变量的存在是优先于对象的
⑤可以通过"类名.类变量"的方式使用
2.static修饰的方法(静态方法或类方法)
①随着类的加载而加载
②静态方法的存在优先于对象
③可用通过"类名.类方法"的方式调用
④静态方法中不能使用非静态成员,非静态方法中可以使用静态成员
(静态方法中能使用静态成员)
⑤静态方法中不能使用this和super
- public class StaticTest {
- public static void main(String[] args) {
- /* Chinese p1=new Chinese("张三",15,"中国");
- Chinese p2=new Chinese("李四",20,"中国");
- p1.setNation("唐朝");//一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
- System.out.println(p1);
- System.out.println(p2);*/
-
- }
- }
- class Chinese{
- private String name;
- private int age;
-
- private static String nation;//描述国籍
- public Chinese() {
- }
- public Chinese(String name, int age, String nation) {
- this.name = name;
- this.age = age;
- this.nation = nation;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
-
- public void setNation(String nation) {
- this.nation = nation;
- }
-
- public String getNation() {
- return nation;
- }
-
- @Override
- public String toString() {
- return "Chinese{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", nation='" + nation + '\'' +
- '}';
- }
- }

static 修饰的属性被该类所有的对象所共享
一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
2.static修饰的方法(静态方法或类方法)
①随着类的加载而加载
②静态方法的存在优先于对象
③可用通过"类名.类方法"的方式调用
④静态方法中不能使用非静态成员,非静态方法中可以使用静态成员
(静态方法中能使用静态成员)
⑤静态方法中不能使用this和super
- public class StaticTest {
- public static void main(String[] args) {
- /* Chinese p1=new Chinese("张三",15,"中国");
- Chinese p2=new Chinese("李四",20,"中国");
- p1.setNation("唐朝");//一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
- System.out.println(p1);
- System.out.println(p2);*/
-
- Chinese.nation="中国";
- System.out.println(Chinese.nation);
- Chinese.show();
- }
- }
- class Chinese{
- private String name;
- private int age;
-
- /*private*/ static String nation;//描述国籍
- public Chinese() {
- }
- public Chinese(String name, int age, String nation) {
- this.name = name;
- this.age = age;
- this.nation = nation;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
-
- public void setNation(String nation) {
- this.nation = nation;
- }
-
- public String getNation() {
- return nation;
- }
- public static void show(){
- System.out.println("这是一个静态方法"+nation);
- }
-
- @Override
- public String toString() {
- return "Chinese{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", nation='" + nation + '\'' +
- '}';
- }
- }


Exer:

- package Static;
-
- public class TestFrock {
- public static void main(String[] args) {
- /*System.out.println(Frock.getNextNum());
- System.out.println(Frock.getNextNum());*/
- Frock f1=new Frock();
- Frock f2=new Frock();
- Frock f3 =new Frock();
- System.out.println(f1.getSerialNumber());
- System.out.println(f2.getSerialNumber());
- System.out.println(f3.getSerialNumber());
- }
- }
- class Frock{
- private static int currentNum=100000;
- private int serialNumber;//序列号
- public Frock(){//构造器
- this.serialNumber=getNextNum();//自动生成衣服的唯一序列号
- }
-
- public static int getNextNum(){
- return currentNum+=100;
- }
-
- public void setSerialNumber(int serialNumber) {
- this.serialNumber = serialNumber;
- }
- public int getSerialNumber() {
- return serialNumber;
- }
- }
