Java中的static关键字

在Java中,static是一个修饰符,可以用来修饰类、变量、方法和代码块。static修饰的成员属于类本身,而不是类的实例。这意味着static成员只有一份,可以在不创建类的对象的情况下直接访问,通过类名.成员名的方式。

static变量

static变量是类变量,它们在类加载时就被分配内存空间,而不是每次创建对象时。static变量的值对所有类的对象是共享的,也就是说,如果一个对象修改了static变量的值,那么其他对象也会看到这个变化。static变量可以用来实现类的常量或全局变量。

例如,下面的代码定义了一个名为PI的static变量,它表示圆周率的值,是一个常量。我们可以在不创建Math类的对象的情况下,直接使用Math.PI来访问这个变量。

public class Math {
    public static final double PI = 3.141592653589793;
}

public class Test {
    public static void main(String[] args) {
        System.out.println("The value of PI is " + Math.PI);
    }
}

输出:

The value of PI is 3.141592653589793

static方法

static方法是类方法,它们可以在不创建类的对象的情况下直接调用,通过类名.方法名的方式。static方法只能访问static变量和static方法,不能访问非static的成员。static方法通常用来实现一些与类相关的工具方法或工厂方法。

例如,下面的代码定义了一个名为square的static方法,它接受一个double类型的参数,返回它的平方值。我们可以在不创建Math类的对象的情况下,直接使用Math.square来调用这个方法。

public class Math {
    public static final double PI = 3.141592653589793;

    public static double square(double x) {
        return x * x;
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println("The square of 2 is " + Math.square(2));
    }
}

输出:

The square of 2 is 4.0

static代码块

static代码块是一段在类加载时就执行的代码,它可以用来初始化static变量或执行一些只需要执行一次的操作。static代码块只会执行一次,无论创建多少个类的对象。static代码块的执行顺序是按照它们在类中的定义顺序,从上到下。

例如,下面的代码定义了两个static代码块,它们分别打印出一句话,并初始化一个static变量。我们可以看到,当我们运行Test类的时候,static代码块会在main方法之前执行,而且只会执行一次。

public class Test {
    static {
        System.out.println("This is the first static block");
        x = 10;
    }

    static int x;

    static {
        System.out.println("This is the second static block");
        x = 20;
    }

    public static void main(String[] args) {
        System.out.println("This is the main method");
        System.out.println("The value of x is " + x);
    }
}

输出:

This is the first static block
This is the second static block
This is the main method
The value of x is 20

static类

static类是嵌套在另一个类中的类,它可以访问外部类的static成员,但不能访问非static的成员。static类可以在不创建外部类的对象的情况下直接创建对象,通过外部类名.内部类名的方式。static类通常用来实现一些与外部类相关的辅助类或逻辑。

例如,下面的代码定义了一个名为Outer的类,它有一个static变量和一个static方法。它还定义了一个名为Inner的static类,它有一个非static变量和一个非static方法。我们可以在不创建Outer类的对象的情况下,直接创建Inner类的对象,通过Outer.Inner的方式。

public class Outer {
    public static int x = 10;

    public static void show() {
        System.out.println("This is the show method of Outer class");
    }

    public static class Inner {
        public int y = 20;

        public void display() {
            System.out.println("This is the display method of Inner class");
            System.out.println("The value of x is " + x);
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Outer.show();
        Outer.Inner obj = new Outer.Inner();
        obj.display();
        System.out.println("The value of y is " + obj.y);
    }
}

输出:

This is the show method of Outer class
This is the display method of Inner class
The value of x is 10
The value of y is 20


案例题目
请分析下面的代码,回答以下问题:

1.这段代码的输出是什么?
2.如果将第10行的static去掉,会有什么变化?
3.如果将第14行的static去掉,会有什么变化?
4.如果将第18行的static去掉,会有什么变化?

public class Test {
    static int x = 10;

    public static void main(String[] args) {
        Test t1 = new Test();
        Test t2 = new Test();
        t1.x = 20;
        t2.x = 30;
        System.out.println(t1.x + " " + t2.x);
        t1.show();
        t2.show();
    }

    static void show() {
        System.out.println("The value of x is " + x);
    }
}

答案:
1.这段代码的输出是:

30 30
The value of x is 30
The value of x is 30


2.如果将第10行的static去掉,会有以下变化:
x变成了实例变量,每个对象都有自己的一份,不再是共享的类变量。
第18行的show方法不能直接访问x,需要通过对象引用来访问,例如this.x或t1.x。
输出变成了:

20 30
The value of x is 20
The value of x is 30

3.如果将第14行的static去掉,会有以下变化:
main方法变成了实例方法,不能作为程序的入口点,需要通过对象引用来调用,例如t1.main(args)。
但是,要创建对象,必须先有main方法,这就造成了一个循环依赖的问题,导致程序无法运行。
因此,这是一个编译错误。
如果将第18行的static去掉,会有以下变化:
show方法变成了实例方法,不能在不创建对象的情况下直接调用,需要通过对象引用来调用,例如t1.show()或t2.show()。
输出不变,仍然是:

30 30
The value of x is 30
The value of x is 30

总结
static关键字是一个在C/C++语言中常用的修饰符,它可以用来修饰变量、函数、代码块和类,以实现不同的目的。static关键字的作用有以下几点:

static变量:static变量可以分为局部变量和全局变量,它们的特点是只有一份存储空间,可以在不创建对象的情况下直接访问,且值在程序运行期间保持不变。static变量可以用来实现常量或全局变量的功能,有利于提高程序的模块化和封装性。

static函数:static函数可以分为类函数和成员函数,它们的特点是只能访问static变量和static函数,不能访问非static的成员。static函数可以用来实现一些与类相关的工具函数或工厂函数,有利于降低程序的耦合度和冲突风险。

static代码块:static代码块是一段在类加载时就执行的代码,它可以用来初始化static变量或执行一些只需要执行一次的操作。static代码块的执行顺序是按照它们在类中的定义顺序,从上到下,且只执行一次。

static类:static类是嵌套在另一个类中的类,它可以访问外部类的static成员,但不能访问非static的成员。static类可以在不创建外部类的对象的情况下直接创建对象,通过外部类名.内部类名的方式。static类通常用来实现一些与外部类相关的辅助类或逻辑。

import static:import static是一个比较冷门的用法,它可以导入一个类的所有static成员,或者指定的static成员,这样就可以省略类名的前缀,直接使用static成员。import static可以提高代码的简洁性和可读性,但也可能造成命名空间的污染和混淆。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值