Java教程15. static关键字
n 掌握static关键字的使用
Java,培训,视频,基础,李兴华,static
在讲解static关键字之前先来看以下的代码,下面定义一个表示A城的人员类
|
class Person{ private String name ; private int age; String city = "A城" ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String getInfo(){ return "姓名:" + this.name + ",年龄:" + this.age + ",城市:" + city ; } }; public class StaticDemo01{ public static void main(String args[]){ Person per1 = new Person("张三",30) ; Person per2 = new Person("李四",31) ; Person per3 = new Person("王五",30) ; System.out.println(per1.getInfo()) ; System.out.println(per2.getInfo()) ; System.out.println(per3.getInfo()) ; } }; |
在本程序中存在以下的问题:
· city属性表示的信息都是一样的,所以对于各个对象来讲内容重复了
· 如果现在假设将A城更名为B城,而且此类已经产生了500个对象,要修改500次。
最好的解决方法是将city属性设置成公共属性。
如果要想将一个属性设置成公共属性,则就需要使用static关键字进行了声明。
|
class Person{ private String name ; private int age; static String city = "A城" ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String getInfo(){ return "姓名:" + this.name + ",年龄:" + this.age + ",城市:" + city ; } }; public class StaticDemo02{ public static void main(String args[]){ Person per1 = new Person("张三",30) ; Person per2 = new Person("李四",31) ; Person per3 = new Person("王五",30) ; System.out.println("============= 信息修改之前 ================") ; System.out.println(per1.getInfo()) ; System.out.println(per2.getInfo()) ; System.out.println(per3.getInfo()) ; System.out.println("============= 信息修改之后 ================") ; per1.city = "B城" ; System.out.println(per1.getInfo()) ; System.out.println(per2.getInfo()) ; System.out.println(per3.getInfo()) ; } }; |
图略,详见视频教程
一般情况下对于使用static声明的属性都使用类名称直接调用,形式如下:
|
类名称.static属性 |
所以,以上的操作代码修改城市属性时应该由Person类完成
|
Person.city = "B城" ; |
Static除了可以声明属性之外,还可以定义方法,使用static定义的方法可以由类名称直接调用。
|
class Person{ private String name ; private int age; private static String city = "A城" ; public static void setCity(String c){ city = c ; } public Person(String name,int age){ this.name = name ; this.age = age ; } public String getInfo(){ return "姓名:" + this.name + ",年龄:" + this.age + ",城市:" + city ; } }; public class StaticDemo03{ public static void main(String args[]){ Person per1 = new Person("张三",30) ; Person per2 = new Person("李四",31) ; Person per3 = new Person("王五",30) ; System.out.println("============= 信息修改之前 ================") ; System.out.println(per1.getInfo()) ; System.out.println(per2.getInfo()) ; System.out.println(per3.getInfo()) ; System.out.println("============= 信息修改之后 ================") ; Person.setCity("B城") ; System.out.println(per1.getInfo()) ; System.out.println(per2.getInfo()) ; System.out.println(per3.getInfo()) ; } }; |
在使用静态方法时需要注意以下几点:
· static的方法只能调用static的属性或方法,不能调用非static的属性或方法。
|
public static void setCity(String c){ city = c ; this.name = "张三" ; this.getInfo() ; } |
那么为什么要有以上的限制呢?
· 静态属性和方法可以在没有实例化对象的时候调用。
· 而类中的普通方法和普通属性,肯定只有在对象实例化之后才有可能调用。
回顾:在最早的时候曾经说过,如果一个方法要想由主方法直接调用,则声明格式为;
|
public static 返回值类型|void 方法名称(){} |
|
public class StaticDemo05{ public static void main(String args[]){ new StaticDemo05().fun() ; } public void fun(){ System.out.println("Hello World!!!") ; } }; |
主方法上也存在static关键字,那么主方法含义是什么?
public static void main(String args[])
· public:表示最大的权限,所有人都可以访问。
· static:因为执行的时候执行的就是类名称,所以表示可以由类名称直接调用
· void:因为主方法是一切的起点,所以表示没有返回值
· main:系统内建的方法名称
· String args[]:表示字符串数组,用于接收参数
|
public class StaticDemo06{ public static void main(String args[]){ for(int x=0;x<args.length;x++){ System.out.println(args[i] + "、") ; } } }; |
那么执行程序的时候要采用以下的格式执行:
· java StaticDemo06 参数1 参数2 参数3 参数…….
如果现在要是想输入“hello world”的字符串。则要使用“"”括起来表示一个完整的参数。
程序的内存划分:
· 栈内存:对象名称,实际上是对象对堆的引用地址
· 堆内存:属性
· 全局代码区:保存所有的操作方法
· 全局数据区:保存所有的static属性