JAVA 静态变量

本文详细探讨了Java中静态变量的声明与初始化过程,包括静态变量的内存位置、初始化方式及顺序,并通过具体代码示例解释了静态变量的初始化机制。
个人的总结
1 静态变量只有一份被类的所有实例共享
2 静态变量的声明在编译时已经明确了内存的位置
3 延迟初始化是改变静态变量的值

[quote]
Java静态变量的初始化(static块的本质)

在网上看到了下面的一段代码:

1. public class Test {
2. static {
3. _i = 20;
4. }
5. public static int _i = 10;
6.
7. public static void main(String[] args) {
8. System.out.println(_i);
9. }
10. }

public class Test { static { _i = 20; } public static int _i = 10; public static void main(String[] args) { System.out.println(_i); } }

上述代码会打印出什么结果来呢?10还是20?本文将以此代码为引子,着重讨论一下静态变量的初始化问题。
问题1:静态变量如何初始化

Java类中可以定义一个static块,用于静态变量的初始化。如:

1. public class Test {
2. public static int _i;
3. static {
4. _i = 10;
5. }
6. }

public class Test { public static int _i; static { _i = 10; } }

当然最常用的初始化静态变量的操作是在声明变量时直接进行赋值操作。如:

1. public class Test {
2. public static int _i = 10;
3. }

public class Test { public static int _i = 10; }

那么上述两例在本质上有什么区别吗?回答是没有区别。两例代码编译之后的字节码完全一致,通过 “javap -c”查看到的字节码如下:

public class Test extends java.lang.Object{
public static int _i;

public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

static {};
Code:
0: bipush 10
2: putstatic #2; //Field _i:I
5: return

}

通过字节码还可以看出,当类的定义中不含有static块时,编译器会为该类提供一个默认的static块。当然这是在含有静态变量初始化操作的前 提下。如果静态变量没有初始化操作,则编译器不会为之提供默认的static块。如:

1. public class Test {
2. public static int _i;
3. }

public class Test { public static int _i; }

其字节码的表现形式为:

public class Test extends java.lang.Object{
public static int _i;

public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

}

由于静态变量是通过赋值操作进行初始化的,因此可以通过静态函数返回值的方式为其初始化。如:

1. public class Test {
2. public static int _i = init();
3.
4. private static int init() {
5. return 10;
6. }
7. }

public class Test { public static int _i = init(); private static int init() { return 10; } }

其本质与下面的代码相同:

1. public class Test {
2. public static int _i;
3. static {
4. _i = init();
5. }
6.
7. private static int init() {
8. return 10;
9. }
10. }

public class Test { public static int _i; static { _i = init(); } private static int init() { return 10; } }

问题2:JDK如何处理static块

类定义中可以存在多个static块吗?回答是可以。如:

1. public class Test {
2. public static int _i;
3. static {
4. _i = 10;
5. }
6.
7. public static void main(String[] args) {
8. }
9.
10. static {
11. _i = 20;
12. }
13. }

public class Test { public static int _i; static { _i = 10; } public static void main(String[] args) { } static { _i = 20; } }

此类编译之后的字节码为:

public class Test extends java.lang.Object{
public static int _i;

public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: return

static {};
Code:
0: bipush 10
2: putstatic #2; //Field _i:I
5: bipush 20
7: putstatic #2; //Field _i:I
10: return

}

观察static{}部分可以看出,上例的代码与下面的代码效果一致:

1. public class Test {
2. public static int _i;
3.
4. public static void main(String[] args) {
5. }
6.
7. static {
8. _i = 10;
9. _i = 20;
10. }
11. }

public class Test { public static int _i; public static void main(String[] args) { } static { _i = 10; _i = 20; } }

此例可以证明,不仅类定义中可以有多个static块,而且在编译时编译器会将多个static块按照代码的前后位置重新组合成一个static 块。
问题3:如何看待静态变量的声明

静态变量存放在常量池之中。如何证明呢?如:

1. public class Test {
2. public static int _i = 10;
3. }

public class Test { public static int _i = 10; }

使用“javap -c -verbose”查看其字节码的内容如下:

public class Test extends java.lang.Object
SourceFile: "Test.java"
minor version: 0
major version: 49
Constant pool:
const #1 = Method #4.#14; // java/lang/Object."<init>":()V
const #2 = Field #3.#15; // Test._i:I
const #3 = class #16; // Test
const #4 = class #17; // java/lang/Object
const #5 = Asciz _i;
const #6 = Asciz I;
const #7 = Asciz <init>;
const #8 = Asciz ()V;
const #9 = Asciz Code;
const #10 = Asciz LineNumberTable;
const #11 = Asciz <clinit>;
const #12 = Asciz SourceFile;
const #13 = Asciz Test.java;
const #14 = NameAndType #7:#8;// "<init>":()V
const #15 = NameAndType #5:#6;// _i:I
const #16 = Asciz Test;
const #17 = Asciz java/lang/Object;

{
public static int _i;


public Test();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 2: 0

static {};
Code:
Stack=1, Locals=0, Args_size=0
0: bipush 10
2: putstatic #2; //Field _i:I
5: return
LineNumberTable:
line 3: 0

}

我们看到,常量池中const #2指向的就是Test._i,也就是静态变量。静态变量被保存到常量池中的工作原理这里不深入讨论。在此需要注意的是:

* 静态变量的声明与初始化是两个不同的操作;
* 静态变量的声明在编译时已经明确了内存的位置。

如:

1. public class Test {
2. public static int _i = 10;
3. }

public class Test { public static int _i = 10; }

上述代码的本质可以视为:

1. public class Test {
2. // 静态变量的声明
3. public static int _i;
4.
5. // 静态变量的初始化
6. static {
7. _i = 10;
8. }
9. }

public class Test { // 静态变量的声明 public static int _i; // 静态变量的初始化 static { _i = 10; } }

由于静态变量的声明在编译时已经明确,所以静态变量的声明与初始化在编码顺序上可以颠倒。也就是说可以先编写初始化的代码,再编写声明代码。如:

1. public class Test {
2. // 静态变量的初始化
3. static {
4. _i = 10;
5. }
6.
7. // 静态变量的声明
8. public static int _i;
9. }

public class Test { // 静态变量的初始化 static { _i = 10; } // 静态变量的声明 public static int _i; }

对初始问题的解答

解答了上述三个问题,让我们再来看看开篇提到的问题。代码如下:

1. public class Test {
2. static {
3. _i = 20;
4. }
5. public static int _i = 10;
6.
7. public static void main(String[] args) {
8. System.out.println(_i);
9. }
10. }

public class Test { static { _i = 20; } public static int _i = 10; public static void main(String[] args) { System.out.println(_i); } }

其本质可以用下面的代码表示:

1. public class Test {
2. static {
3. _i = 20;
4. }
5. public static int _i;
6. static {
7. _i = 10;
8. }
9.
10. public static void main(String[] args) {
11. System.out.println(_i);
12. }
13. }

public class Test { static { _i = 20; } public static int _i; static { _i = 10; } public static void main(String[] args) { System.out.println(_i); } }

再简化一下,可以表示为:

1. public class Test {
2. public static int _i;
3.
4. static {
5. _i = 20;
6. _i = 10;
7. }
8.
9. public static void main(String[] args) {
10. System.out.println(_i);
11. }
12. }

public class Test { public static int _i; static { _i = 20; _i = 10; } public static void main(String[] args) { System.out.println(_i); } }

至此,代码已经明确告诉我们打印结果是什么了!

[/quote]
### Java 静态变量使用指南与注意事项 Java 中的静态变量(Static Variable)是类的一个重要特性,它们属于类本身而不是类的实例。因此,无论创建多少个对象,静态变量在内存中只有一个副本[^1]。以下是关于静态变量的详细说明和使用指南: #### 1. 静态变量的定义 静态变量通过 `static` 关键字修饰,声明在类中但不隶属于任何特定对象。例如: ```java public class Example { public static int count = 0; // 静态变量 } ``` 静态变量在整个程序运行期间都存在,并且可以通过类名直接访问 `Example.count`,而无需创建类的实例[^1]。 #### 2. 静态变量的作用域 静态变量的作用域与普通成员变量类似,但它们在整个类的所有实例之间共享。如果一个实例修改了静态变量的值,其他实例也会看到这一变化。例如: ```java public class Example { public static int count = 0; public Example() { count++; } public static void main(String[] args) { new Example(); new Example(); System.out.println(Example.count); // 输出:2 } } ``` #### 3. 初始化方式 静态变量可以在声明时初始化,也可以在静态代码块中初始化。静态代码块仅在类加载时执行一次。例如: ```java public class Example { public static int count; static { count = 10; // 在静态代码块中初始化 } } ``` #### 4. 注意事项 - **线程安全**:由于静态变量被所有实例共享,多线程环境下可能会引发竞争条件(Race Condition)。在这种情况下,应考虑使用同步机制或线程安全的数据结构[^1]。 - **内存管理**:静态变量在整个应用程序生命周期内都占用内存,即使没有任何对象引用该类,静态变量仍然存在。因此,应避免将大对象存储为静态变量,以免浪费内存。 - **类加载机制**:静态变量在类加载时初始化,如果类未被加载,静态变量也不会被初始化[^2]。 #### 5. 实际开发中的应用 静态变量通常用于以下场景: - 存储全局配置信息,例如数据库连接字符串、API密钥等。 - 计数器功能,例如统计创建的对象数量。 - 缓存常用数据,例如静态方法的结果或常量值。 #### 示例代码 以下是一个简单的例子,展示如何使用静态变量来统计对象的数量: ```java public class Person { private String name; public static int personCount = 0; // 静态变量 public Person(String name) { this.name = name; personCount++; // 每次创建对象时递增 } public static void printPersonCount() { System.out.println("Total persons: " + personCount); } public static void main(String[] args) { new Person("Alice"); new Person("Bob"); Person.printPersonCount(); // 输出:Total persons: 2 } } ``` ### 结论 通过合理使用静态变量,可以简化程序设计并提高代码的可维护性。然而,在使用静态变量时需要注意其线程安全性、内存消耗以及与其他类或对象的交互[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值