类的static

本文深入探讨Java中的static关键字,解释其如何使属性或方法被所有对象共享,介绍内存区域分配,并提供实例展示如何使用static来统计对象实例数量及自动编号。
在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+"个");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值