一:在java中可以使用static声明属性或方法,因为初学者在开始学习Java时,多数使用的属性和方法都属于非static的,这样一来,每个对象都占有各自的内容空间,如果现在希望一个属性被所有的对象所拥有,则可以将其声明为static类型,声明为static类型的属性或方法之后的此属性也被称为类方法!可以由类名直接调用;
注意:静态static属性或方法不能用this来调用,因为this代表的是当前对象,而static代表的是全局数据区!
Java中常见4种区域:
(1)栈内存:可以保存对象的名称(保存,访问的堆内存地址)
(2)堆内存:可以保存每个对象具体属性
(3)全局数据区:保存static类型的属性
(4)全局代码区:保存所有方法的定义
二:static关键字的用法详解
(1)使用static声明全局属性
比如有1000个对象,输出某一个相同的信息,那么这时候用static最合适
举例:修改一个对象中的country信息,则其他对象中country信息也被改变了,则说明了所有对象共享此属性!
public class Static {
public static void main(String[] args) {
Person9 per1 = new Person9("nishuibaichuan", 21);
Person9 per2 = new Person9("huohu", 23);
Person9 per3 = new Person9("zhanlang", 24);
System.out.println("===========修改之前============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
per1.country = "B城市";//或者Person9.country = "B城市";
System.out.println("===========修改之后============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
}
}
class Person9{
String name ;//暂不封装
int age;
static String country = "A城市";
public Person9(String name,int age){
this.name = name;
this.age = age;
}
public void getInfo(){
System.out.println("姓名:" + name +",年纪:" + age
+ ",所在城市:" + country);
}
}
运行结果:
===========修改之前============
姓名:nishuibaichuan,年纪:21,所在城市:A城市
姓名:huohu,年纪:23,所在城市:A城市
姓名:zhanlang,年纪:24,所在城市:A城市
===========修改之后============
姓名:nishuibaichuan,年纪:21,所在城市:B城市
姓名:huohu,年纪:23,所在城市:B城市
姓名:zhanlang,年纪:24,所在城市:B城市
每一个对象都有自己的堆栈空间,堆内存空间中保存每个对象的各自属性,但是所有的static属性保存在了全局数据区中,所有的对象指向全局数据区中的一个内容,所以当一个对象修改之后,那么所有对象的内容将全部变换。
如下图:
在调用static属性的时候,最好是使用"类名称.属性"的形式调用.
(2)使用static声明方法
如果一个方法使用了static关键字声明,则此方法可以直接使用类名称进行调用,以下将上面(1)中代码中的属性进行封装。
举例:
public class Static1 {
public static void main(String[] args) {
Person10 per1 = new Person10("nishuibaichuan", 21);
Person10 per2 = new Person10("huohu", 23);
Person10 per3 = new Person10("zhanlang", 24);
System.out.println("===========修改之前============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
Person10.setCoutry("C城市");
System.out.println("===========修改之后============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
}
}
class Person10{
private String name ;
private int age;
private static String country = "A城市";
public static void setCoutry(String c){
country = c;
}
public static String getCoutry(){
return country;
}
public Person10(String name,int age){
this.name = name;
this.age = age;
}
public void getInfo(){
System.out.println("姓名:" + name +",年纪:" + age
+ ",所在城市:" + country);
}
}
运行结果:
===========修改之前============
姓名:nishuibaichuan,年纪:21,所在城市:A城市
姓名:huohu,年纪:23,所在城市:A城市
姓名:zhanlang,年纪:24,所在城市:A城市
===========修改之后============
姓名:nishuibaichuan,年纪:21,所在城市:C城市
姓名:huohu,年纪:23,所在城市:C城市
姓名:zhanlang,年纪:24,所在城市:C城市
注意:使用static静态方法,不能调用非static静态属性或方法,而非static静态属性或方法,可以调用static静态方法和非static静态属性或方法!具体验证请读者下去验证即可。
为什么不能调用啊?因为static属性或方法可以在对象没有实例化的时候就直接进行调用了。而非static静态属性或方法要在对象实例化之后才能进行调用!否则会报错,如果我们非要用静态static方法调用非静态static方法,可以用“对象.属性或方法”的形式.这样就可以调用非静态属性或方法了.
举例1:验证对象没有实例化的时候就直接进行调用了代码如下
public class Static3 {
public static void main(String[] args) {
Person11.country = "D城市";//对象没有实例化的时候就直接进行调用了
Person11 per1 = new Person11("nishuibaichuan", 21);
Person11 per2 = new Person11("huohu", 23);
Person11 per3 = new Person11("zhanlang", 24);
System.out.println("===========修改之前============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
System.out.println("===========修改之后============");
per1.getInfo();
per2.getInfo();
per3.getInfo();
}
}
class Person11{
String name ;//暂不封装
int age;
static String country = "A城市";
public Person11(String name,int age){
this.name = name;
this.age = age;
}
public void getInfo(){
System.out.println("姓名:" + name +",年纪:" + age
+ ",所在城市:" + country);
}
}
运行结果:
===========修改之前============
姓名:nishuibaichuan,年纪:21,所在城市:D城市
姓名:huohu,年纪:23,所在城市:D城市
姓名:zhanlang,年纪:24,所在城市:D城市
===========修改之后============
姓名:nishuibaichuan,年纪:21,所在城市:D城市
姓名:huohu,年纪:23,所在城市:D城市
姓名:zhanlang,年纪:24,所在城市:D城市
举例2:静态static方法调用非静态static方法代码如下
public class Static6 {
public static void main(String[] args) {
Person12.country = "D城市";
Person12 per2 = new Person12("nishuibaichuan", 21);
Person12.getInfo(per2);//静态方法可以类名.方法名()形式调用
}
}
class Person12{
private String name ;
private int age;
static String country = "A城市";
public Person12(String name,int age){
this.name = name;
this.age = age;
}
public static void getInfo(Person12 per){
System.out.println("姓名:" + per.name +",年纪:" + per.age
+ ",所在城市:" + country);
}
}
运行结果:
姓名:nishuibaichuan,年纪:21,所在城市:D城市
(3)static的其他应用
(1)举例一
从上面知道static属性是所有对象共享的,那么就可以使用static属性统计一个类产生了多少个实例化对象
思路:因为每一个实例化对象在操作的时候,必须调用构造方法,所以如果现在要进行统计的话,则直接在构造方法中增加一个static属性即可
代码如下:
public class Static4 {
public static void main(String[] args) {
new Demo();//以下为增加的新对象
new Demo();
new Demo();
new Demo();
new Demo();
}
}
class Demo{
private static int count = 0;
public Demo(){
count++;//只要有对象产生,则count会自增
System.out.println("产生了" + count + "个对象!");
}
}
运行结果:
产生了1个对象!
产生了2个对象!
产生了3个对象!
产生了4个对象!
产生了5个对象!
(2)举例二
可以使用static为对象进行自动的编名操作,此操作与上面代码类似。
代码如下:
public class Static5 {
public static void main(String[] args) {
System.out.println(new Demo1().getName());
System.out.println(new Demo1("NISHUI").getName());
System.out.println(new Demo1().getName());
System.out.println(new Demo1("BAICHUAN").getName());
System.out.println(new Demo1().getName());
}
}
class Demo1{
private String name;
private static int count = 0;
public Demo1(){
count++;//只要有对象产生,则count会自增
this.name = "DEMO1--" + count;//自动进行编名操作
}
public Demo1(String name){
this.name = name;//通过构造方法赋值
}
public String getName(){
return this.name;
}
}
运行结果:
DEMO1--1
NISHUI
DEMO1--2
BAICHUAN
DEMO1--3