java学习笔记(十六)

static的用法:


1.1:

使用static声明属性或方法,因为之前所讲解的属性和方法都是非static的,这样就导致了每个对象都有各自的内容。

如果 现在希望一个属性被所有对象所拥有,则可以将其声明为static类型。

A:
static可以申明全局属性,


class Person
{
String name;
int age;
String country="A城";
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名: "+this.name+", 年龄: "+this.age+",城市:"+country);
}

};
public class StaticDemo01 {
public static void main(String args[]){
Person p1= new Person("zhangsan",20);
Person p2= new Person("lisi",30);

Person p3= new Person("zhenhan",10);
p1.info();
p2.info();
p3.info();

}
};

结果为;

F:\java>java StaticDemo01
姓名: zhangsan, 年龄: 20,城市:A城
姓名: lisi, 年龄: 30,城市:A城
姓名: zhenhan, 年龄: 10,城市:A城


但是,以上代码确实实现了一些基本的功能,但是如果要修改城市,并且产生了5000个对象,那么意味着此时意味着需要修改5000对象中的country属性,所以,此时最好使用关键字声明属性。

class Person
{
String name;
int age;
static String country="A城";
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名: "+this.name+", 年龄: "+this.age+",城市:"+country);
}

};
public class StaticDemo01 {
public static void main(String args[]){
Person p1= new Person("zhangsan",20);
Person p2= new Person("lisi",30);
Person p3= new Person("zhenhan",10);
System.out.println("---------修改之前---");
p1.info();
p2.info();
p3.info();
System.out.println("---------修改之后---");
p1.country="B城";
p1.info();
p2.info();
p3.info();
}
};


结果为:

F:\java>java StaticDemo01
---------修改之前---
姓名: zhangsan, 年龄: 20,城市:A城
姓名: lisi, 年龄: 30,城市:A城
姓名: zhenhan, 年龄: 10,城市:A城
---------修改之后---
姓名: zhangsan, 年龄: 20,城市:B城
姓名: lisi, 年龄: 30,城市:B城
姓名: zhenhan, 年龄: 10,城市:B城


可见:
修改一个对象中的country属性,则其他对象的country属性的内容全部改变,证明此属性是所有对象所共享的。


每一个对象都拥有各自的堆栈空间,堆内存空间中保存内阁对象的个子属性,但所有的static属性是保存在全局数据区中,所有的对象指向全局数据区的一个内容。


那么java中到底有多少个内存区域呢?

@ 栈内存:可以保存对象的名称,(保存访问的堆内存的地址)

@ 堆内存:保存每个对象的具体属性

@ 全局数据区:保存static类型的属性

@ 全局代码区:保存所有方法的定义


注意:

一般在调用static属性的时候最好是使用类名称直接调用,采用“类名称.属性”的形式调用。


class Person
{
String name;
int age;
static String country="A城";
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名: "+this.name+", 年龄: "+this.age+",城市:"+country);
}

};
public class StaticDemo01 {
public static void main(String args[]){
Person p1= new Person("zhangsan",20);
Person p2= new Person("lisi",30);
Person p3= new Person("zhenhan",10);
System.out.println("---------修改之前---");
p1.info();
p2.info();
p3.info();
System.out.println("---------修改之后---");
//p1.country="B城";
Person.country="B城";
p1.info();
p2.info();
p3.info();
}
};


F:\java>java StaticDemo01
---------修改之前---
姓名: zhangsan, 年龄: 20,城市:A城
姓名: lisi, 年龄: 30,城市:A城
姓名: zhenhan, 年龄: 10,城市:A城
---------修改之后---
姓名: zhangsan, 年龄: 20,城市:B城
姓名: lisi, 年龄: 30,城市:B城
姓名: zhenhan, 年龄: 10,城市:B城


1.2:声明static方法


如果一个方法使用了static关键字声,则此方法可以直接使用类名称进行调用。


class Person
{
private String name;
private int age;
private static String country="A城";
public static void setCountry(String c){
country = c;
}
public static String getCountry(){
return country;
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名: "+this.name+", 年龄: "+this.age+",城市:"+country);
}

};
public class StaticDemo01 {
public static void main(String args[]){
Person p1= new Person("zhangsan",20);
Person p2= new Person("lisi",30);
Person p3= new Person("zhenhan",10);
System.out.println("---------修改之前---");
p1.info();
p2.info();
p3.info();
System.out.println("---------修改之后---");
//p1.country="B城";
Person.setCountry("B城");
p1.info();
p2.info();
p3.info();
}
};

注意点:

使用static方法,不能调用飞static的属性或方法。

实际上这一点非常好理解,因为static属性或方法看可以在对象内有实例化的时候就直接进行调用了。

1.3:static其他应用


可以使用static属性统计一个类到底产生了多少个实例化对象。

因为每一个实例化对象在操作的时候都必须调用构造方法完成,所以如果进行统计的话,则直接在构造方法中增加一个static的属性即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值