且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中静态变量和全局变量之间的区别

更新时间:2022-12-24 10:13:55

你提到的问题有点困惑标题中的static / global,以及问题中的global / local。

Your question is a little confused since you refer to static/global in the title, and global/local in your question.

static 变量绑定到 class ,每个类你将有一个实例

static variables are tied to a class, and you will have one instance per class.

类可以有成员变量,你将拥有该类的每个实例一个实例。

classes can have member variables, and you will have one instance per instance of that class.

请注意,如果您有多个类加载器,这会更复杂。在这种情况下,您可以加载多个类定义,因此可能有多个静态变量。

Note that this is complicated further if you have multiple classloaders. In this scenario you can have multiple class definitions loaded, and consequently, possible multiple static variables.