Java学习—static关键字

Java中的static关键字主要用于内存管理。我们可以应用在变量、方法、块和嵌套类中。static关键字属于类,而不是类的实例。
静态(static)可以是:

  • 变量(也称为类变量)
  • 方法(也称为类方法)
  • 代码块
  • 嵌套类

1.Java静态变量

如果将一个变量声明为static,它就是所谓的静态变量了。
- 静态变量可以用于引用所有对象的公共属性(对于每个对象不是唯一的)。如:员工公司名称,学生所在的大学名称。
优点:它能使程序存储器高效(即它节省内存)。
理解不使用静态变量的问题

//Program of static variable  

class Student8 {
    int rollno;
    String name;
    static String college = "ITS"; //静态实例变量

    Student8(int r, String n) {
        rollno = r;
        name = n;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + college);
    }

    public static void main(String args[]) {
        Student8 s1 = new Student8(111, "Karan");
        Student8 s2 = new Student8(222, "Aryan");

        s1.display();
        s2.display();
    }
}

假设在一所学校有500名学生,现在所有实例数据成员将在每次创建对象时获取内存。所有学生都有其唯一的注册ID:rollno和 name ,因此实例数据成员没有什么问题。college 指的是所有对象的共同属性。如果使它静态化(使用static关键字修饲),这个字段将只获得内存一次。

Java静态属性被共享给所有对象。
静态变量的实例

//Program of static variable  

class Student8 {
    int rollno;
    String name;
    static String college = "ITS";

    Student8(int r, String n) {
        rollno = r;
        name = n;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + college);
    }

    public static void main(String args[]) {
        Student8 s1 = new Student8(111, "Karan");
        Student8 s2 = new Student8(222, "Aryan");

        s1.display();
        s2.display();
    }
}

创建对象示例图如下图所示
这里写图片描述
不使用静态变量的计数器程序
在这个例子中,我们创建了一个名为count的实例变量用来统计创建对象的数目,它在构造函数中执行递增。 由于实例变量在创建对象时要获取内存,每个对象都将具有实例变量的副本,如果它被递增了,它也不会反映到其他对象中。所以每个对象在count变量中的值还是1。

class Counter {
    int count = 0;// will get memory when instance is created

    Counter() {
        count++;
        System.out.println(count);
    }

    public static void main(String args[]) {

        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

    }
}

计数器静态变量的程序
静态变量将只获取一次内存,如果任何对象更改静态变量的值,它将保留其值,所有实例均可访问同一变量值。

class Counter2 {
    static int count = 0;// will get memory only once and retain its value

    Counter2() {
        count++;
        System.out.println(count);
    }

    public static void main(String args[]) {

        Counter2 c1 = new Counter2();
        Counter2 c2 = new Counter2();
        Counter2 c3 = new Counter2();

    }
}

2.Java静态方法
如果在任何方法上应用static关键字,此方法称为静态方法。

  • 静态方法属于类,而不属于类的对象
  • 可以直接调用静态方法,而无须创建类的实例
  • 静态方法可以访问静态数据成员,并可以更改静态数据成员的值。

静态方法示例

//Program of changing the common property of all objects(static field).  

class Student9 {
    int rollno;
    String name;
    static String college = "ITS";

    static void change() {
        college = "BBDIT";
    }

    Student9(int r, String n) {
        rollno = r;
        name = n;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + college);
    }

    public static void main(String args[]) {
        Student9.change();

        Student9 s1 = new Student9(111, "Karan");
        Student9 s2 = new Student9(222, "Aryan");
        Student9 s3 = new Student9(333, "Sonoo");

        s1.display();
        s2.display();
        s3.display();
    }
}

静态方法的限制
主要有以下两个限制,它们分别是:

  • 静态方法不能直接使用非静态数据成员或调用非静态方法。
  • this和super两个关键字不能在静态上下文中使用。
class A {
    int a = 40;// non static

    public static void main(String args[]) {
        System.out.println(a);
    }
}

上述代码会出现编译错误,这是因为对象不需要调用静态方法,如果main函数是非静态方法,jvm首先要创建对象,然后调用main()方法,这将导致额外的内存分配的问题。
3.Java静态块
Java中的静态块主要有两个作用:

  • 用于初始化静态数据成员
  • 它在类加载时在main方法之前执行

示例:

class A2 {
    static {
        System.out.println("static block is invoked");
    }

    public static void main(String args[]) {
        System.out.println("Hello main");
    }
}

输出:

static block is invoked
Hello main
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

li_jeremy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值