您的位置:首页 > 其它

static关键字

2016-04-11 22:57 218 查看
java中,static关键字可以用于定义属性及方法

1.使用static定义属性

我们已经知道在一个类中,主要组成就是属性和方法(分为构造方法与普通方法),而每一个对象都分别拥有各自的属性内容(不同对象的属性保存在不同的堆内存中),如果现在类中的某个属性想要定义为公共属性(即所有对象都可以使用该属性)则可以在声明属性前加上static关键字。

package com.test9;

public class TestStatic {

public static void main(String[] args) {
Person per1=new Person("星星", 200);
Person per2=new Person("月亮", 400);
Person per3=new Person("太阳", 800);
per1.country="燕京";
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());

}

}

class Person{
private String name;
private int age;
static String country="北京";
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String getInfo(){
return "姓名:"+this.name+",年纪:"+this.age+",城市:"+this.country;

}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getCountry() {
return country;
}
public static void setCountry(String country) {
Person.country = country;
}
}
在主类中某一个对象修改了country属性内容后,多有对象的country属性就全部改变了。输出结果如下:

姓名:星星,年纪:200,城市:燕京

姓名:月亮,年纪:400,城市:燕京

姓名:太阳,年纪:800,城市:燕京

PS:常用内存区域:

1.栈内存空间,保存所有的对象名称(准确说是保存了引用的堆内存空间的地址)

2.堆内存空间,保存每个对象的具体属性内容

3.全局数据区,保存static类型的属性

4.全局代码区,保存所有的方法定义

修改上面的代码,按照“类名称.static.属性”的方法来完成。

/***
* 改写上面的代码,上面是由一个对象来修改的,现在改为由所有对象的公共代表---类进行操作
* static属性最好通过类名称来进行调用,使用“类名称.static属性”
*/
public class TestStatic {
public static void main (String args[]){
Person per1=new Person("张三", 23);
Person.country="燕京";  //直接使用我们的类名称调用static属性
System.out.println(Person.country);
System.out.println(per1.getInfo());
}
}

class Person{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
static String country="北京";//未封装!!!!!!
public Person(String name,int age){
this.name=name;
this.age=age;

}
public String getInfo(){
return "姓名:"+name+",年纪:"+age+",城市:"+this.country;
}
}


总结:使用static定义的属性不在堆内存中保存,而是保存在全局数据区;

使用static定义的属性表示的是类属性,类属性可以由类名称直接进行调用(上面的案例就是);

static属性虽然定义在类中,但是其可以在没有实例化对象时进行调用(普通属性保存在堆内存中,而static属性保存在全局数据区中);

2.使用static定义方法

使用static定义的方法也可以在没有实例化对象产生的情况下由类名称直接进行调用

public class TestStatic {
public static void main (String args[]){
Person per1=new Person("张三", 23);

Person.setCountry("燕京");
System.out.println(Person.getCountry());
System.out.println(per1.getInfo());
}
}

class Person{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getCountry() {
return country;
}
public static void setCountry(String country) {
Person.country = country;
}
private String name;
private int age;
private static String country="北京"; //封装
public Person(String name,int age){
this.name=name;
this.age=age;

}
public String getInfo(){
return "姓名:"+name+",年纪:"+age+",城市:"+this.country;
}
}


注意!!

static定义的方法不能调用非static的方法或属性;

非static定义的方法可以调用static的属性或方法

3.理解主方法

如果一个方法在主类中定义,并且由主方法直接调用时,其前面必须有public static,如下格式:

public static 返回值类型 方法名称(参数列表){

[return [返回值];]

}

!!!!!!牢记,所有的非static方法几乎都有一个特点:方法要由实例化对象调用。

下面请看代码:

public class TestStatic {
public  static void main(String args[]){
print();   //直接调用
}

private static void print() {
System.out.println("Hello World!");

}
}
下面我们删掉print()方法的static,就必须使用实例化对象来调用非static方法

public class TestStatic {
public  static void main(String args[]){
new TestStatic().print();   //对象调用
}

private   void print() {   //非static方法
System.out.println("Hello World!");

}
}


暂时先写到这里,后面我还会继续补充······
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: