首页 > 代码库 > java-Static关键字

java-Static关键字

1.使用static关键字声明的属性为全局属性

未使用static关键字指定city之前,如果需要将Tom,Jack,Mary三人的城市均改成Beijing,需要再次声明三次对象的city为Beijing

 

 1 package packageone;
 2 class People{
 3     String name;
 4     String city  = "Shanghai";
 5     public People(String name) {
 6         this.name = name;
 7     }
 8     public void showInfo() {
 9         System.out.println("姓名:"+name+","+"城市:"+city);
10     }
11 }
12 public class StaticDemo {
13 
14     public static void main(String[] args) {
15         People p1 = new People("Tom");
16         p1.city = "Beijing";
17         p1.showInfo();
18         People p2 = new People("Jack");
19         p2.city = "Beijing";
20         p2.showInfo();
21         People p3 = new People("Mary");
22         //未声明p3对象的city属性为Beijing,则其city仍为Shanghai。
23         p3.showInfo();
24     }
25 }

 

使用static关键字指定city后,只需设置city = “Beijing”一次,即可实现三个人城市的更改

 1 package packageone;
 2 class People{
 3     String name;
 4     static String city  = "Shanghai";
 5     public People(String name) {
 6         this.name = name;
 7     }
 8     public void showInfo() {
 9         System.out.println("姓名:"+name+","+"城市:"+city);
10     }
11 }
12 public class StaticDemo {
13 
14     public static void main(String[] args) {
15         People p1 = new People("Tom");
16         p1.city = "Beijing";
17         p1.showInfo();
18         People p2 = new People("Jack");
19         p2.showInfo();
20         People p3 = new People("Mary");
21         //未声明p3对象的city属性为Beijing,但其city变为Beijing。
22         p3.showInfo();
23     }
24 }

2.使用static关键字声明的属性和方法可直接通过类名来调用(代码作为对该static应用的解释有点复杂了,同时是接口的简单使用)

 

 1 package packageone;
 2 
 3 interface USB {
 4     void start();
 5     void stop();
 6 }
 7 
 8 class C {
 9     public static void work(USB u) {
10         u.start();
11         System.out.println("工作中");
12         u.stop();
13     }
14 }
15 
16 class USBDisk implements USB {
17     @Override
18     public void start() {
19         System.out.println("U盘开始工作");
20     }
21 
22     @Override
23     public void stop() {
24         System.out.println("U盘停止工作");
25     }
26 }
27 
28 class Printer implements USB {
29     @Override
30     public void start() {
31         System.out.println("打印机开始工作");
32     }
33 
34     @Override
35     public void stop() {
36         System.out.println("打印机停止工作");
37     }
38 }
39 
40 public class interfacetest {
41     public static void main(String[] args) {
42         //直接通过类名来调用work方法
43         C.work(new USBDisk());
44         C.work(new Printer());
45     }
46 
47 }

 

3.注意:】使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的。而非static声明的方法是可以去调用static声明的属性或方法

 1 package packageone;
 2 //由于博主我水平有限参考了别人的代码案例,但由于他的代码有较大错误,经调试成功后援引,算是自己的代码了吧~嘿嘿
 3 class People {
 4     private String name;
 5     private int age;
 6 
 7     public String getName() {
 8         return name;
 9     }
10 
11     public void setName(String name) {
12         this.name = name;
13     }
14 
15     public int getAge() {
16         return age;
17     }
18 
19     public void setAge(int age) {
20         this.age = age;
21     }
22 
23     // 使用static定义country属性
24     private static String country = "China";
25 
26     // 定义static方法,修改static属性
27     public static void setCountry(String c) {
28         country = c;
29     }
30 
31     // 取得static属性
32     public static String getCountry() {
33         return country;
34     }
35 
36     // 通过构造方法为属性赋值(初始化操作)
37     public People(String name, int age) {
38         this.name = name;
39         this.age = age;
40     }
41 
42     public void info() {
43         System.out.println("姓名:"+ getName()+"年龄:"+getAge()+"城市:"+country);
44     }
45 }
46 
47 public class StaticDemo {
48     public static void main(String args[]) {
49         People per1 = new People("张三", 20);
50         People per2 = new People("李四", 21);
51         People per3 = new People("王五", 23);
52         System.out.println("--------- 修改前-----------");
53         per1.info();
54         per2.info();
55         per3.info();
56         System.out.println("--------- 修改后-----------");
57         // 直接使用类名称调用方法来修改static属性的内容,正是因为country为static全局变量,才不需要每个人都去修改国籍
58         People.setCountry("USA");
59         per1.info();
60         per2.info();
61         per3.info();
62     }
63 }

 

java-Static关键字