现在的位置: 首页 > 综合 > 正文

类的static

2013年09月09日 ⁄ 综合 ⁄ 共 2534字 ⁄ 字号 评论关闭

在java 中可以使用static声明属性或方法,因为在之前所讲解的属性和方法非static,这样一来,每个对象都占有各自己的内容,如果现希望每个属性被对象所共同拥有,则可以将其声明为static类型,声明static类型属性或方法之后,此属性或方法也称为类方法,可以由类方法直接调用。

普通类:

class Person{
    public String name;
    public int age;
    public String country="A城";
    public void Person(String name,int age){
        this.name = name;
        this.age = age;    
    }
    public void info(){
        System.out.println("name = "+this.name+",age = "+this.age+",country = "+this.country);
    }
}
public class StaticDemo01{
    public static void main(String args[]){
        Person p1 = new Person("张三",30);
        Person p2 = new Person("李四",31);
        Person p3 = new Person("王五",32);
        p1.info();
        p2.info();
        p3.info();    
    }
}

假设此时生成了5000个Person对象而全部一次性完全做更改怎么办?

修改如下:

class Person{
    public String name;
    public int age;
    public static String country="A城";
    public void Person(String name,int age){
        this.name = name;
        this.age = age;    
    }
    public void info(){
        System.out.println("name = "+this.name+",age = "+this.age+",country = "+this.country);
    }
}
public class StaticDemo01{
    public static void main(String args[]){
        Person p1 = new Person("张三",30);
        Person p2 = new Person("李四",31);
        Person p3 = new Person("王五",32);
        p1.country = "B城";
        p1.info();
        p2.info();
        p3.info();    
    }
}

//输出为:
//name = 张三,age = 30, country = B城
//name = 李四,age = 31, country = B城
//name = 王五,age = 32, country = B城

证明country属性是所有对象共享的。

拓展:到底有多少内存区域呢?
栈内存:可以保存对象名称。
堆内存:保存每个对象的属性。
全局数据区:保存static类型属性。
全局代码区:保存所有方法定义。

一般在调用static属性时最好是使用类名称直接调用,采用“类名称.属性”的形式调用。
Person.country = "B区域";

定义static方法:

class Person{
    public String name;
    public int age;
    private static String country="A城";
    public void Person(String name,int age){
        this.name = name;
        this.age = age;    
    }
    public void info(){
        System.out.println("name = "+this.name+",age = "+this.age+",country = "+this.country);
    }
    public static void setCountry(String c){
        country = c;
    }
}
public class StaticDemo01{
    public static void main(String args[]){
        Person p1 = new Person("张三",30);
        Person p2 = new Person("李四",31);
        Person p3 = new Person("王五",32);
        Person.setCountry("B城");
        p1.info();
        p2.info();
        p3.info();    
    }
}

//输出为:
//name = 张三,age = 30, country = B城
//name = 李四,age = 31, country = B城
//name = 王五,age = 32, country = B城

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

non-static variable name cannot be referenced from a static context
non-static variable fun() cannot be referenced from a static context

因为static属性或方法可以在对象未实例化的时候直接进行调用,非static方法或属性需在实例化之后才能使用。

static的其他应用:

范例一:可以统计一个类到底产生了多少实例化对象。

public class Test{
    public static void main(String [] args){
        new Demo();
        new Demo();
        new Demo();
    }
}

class Demo{
    private static int num=0;

    public Demo(){
        num++;
        System.out.println("实例化了"+num+"个对象");
    }
}

范例二:为对象进行自动的编名操作。

class Student
{
    static int count=0;
    String name;
    public Student(String name)
    {
        this.name=name;
        count++;
    }
}
class Test
{
    public static void main(String[] args)
    {
        Student stu;
        for(int i=0;i<100;i++)
        {
            stu=new Student("obj_"+(i+1));
            System.out.println("构造"+stu.name+"                   该对象为第"+stu.count+"个");
        }
    }
}

抱歉!评论已关闭.