java Java中静态变量和方法的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4403031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scope of static variable and methods in Java
提问by eternal
I have some doubts about usage of static method in Java. I read many places static variables are instance independent so be comes global.
我对 Java 中静态方法的使用有一些疑问。我读过很多地方静态变量是独立于实例的,所以是全局变量。
public class ThirdClass {
public static var = "Java";
}
public class Second {
public static void main(String[] args) {
ThirdClass ob1 = new ThirdClass();
System.out.println(ob1.var); // prints Java
ob1.var="Ruby";
ThirdClass ob2 = new ThirdClass();
System.out.println(ob2.var); // prints Ruby
}
}
public class First {
public static void main(String[] args) {
ThirdClass ob3 = new ThirdClass();
System.out.println(ob1.var); // prints Java again!!!
}
}
As you see in second class multiple instance of ThirdClass sharing same instance of variable var. But a separate instance in class First don't access the final value "Ruby" but show original "Java". It means the static variable are NOT global variable but only global to single execution!!!
正如您在第二类中看到的,ThirdClass 的多个实例共享变量 var 的相同实例。但是 First 类中的一个单独实例不访问最终值“Ruby”而是显示原始“Java”。这意味着静态变量不是全局变量,而只是单个执行的全局变量!!!
Also do is creating static variable resource intensive compared to instance variable?
与实例变量相比,创建静态变量资源密集吗?
Please suggest.
请建议。
回答by cdhowie
It means the static variable are NOT global variable but only global to single execution!!!
这意味着静态变量不是全局变量,而只是单个执行的全局变量!!!
Of course they are. Allvariables that are not persisted to some kind of storage (like the hard disk) do not retain their values between distinct executions of the program.
他们当然是。 所有没有持久化到某种存储(如硬盘)的变量在程序的不同执行之间不会保留它们的值。
回答by Robin
The value is initialized when the class is loaded. Therefore each time you execute the code, it is initialized to the value "Java" as is defined in the class. The new value is not saved, it is only changed in memory and is "reset" when the code is executed again.
该值在加载类时初始化。因此,每次执行代码时,它都会初始化为类中定义的值“Java”。新值不会保存,只会在内存中更改,并在再次执行代码时“重置”。
The term global has nothing to do with the variables persistence, and scope is defined only within the running program.
术语全局与变量持久性无关,作用域仅在正在运行的程序中定义。
回答by supernova
@eternal I think I am getting the point you wanna ask. I tested this (with some minor compile changes) on Jboss. The results were: Once deployed the scope of class ThirdClass seems to be application deployment level. And the static value of var was retained while multiple method calls.
@eternal 我想我明白你想问的了。我在 Jboss 上测试了这个(有一些小的编译更改)。结果是:一旦部署,类 ThirdClass 的范围似乎是应用程序部署级别。并且在多个方法调用时保留了 var 的静态值。
Here is the basic structure i used.
这是我使用的基本结构。
public class ThirdClass {
public static var = "Java";
}
public class Second class{
public void testA {
ThirdClass ob1 = new ThirdClass(); // not needed , just kept for clarity.
System.out.println(ThirdClass.var);
ThirdClass.var="Ruby";
ThirdClass ob2 = new ThirdClass();
System.out.println(ThirdClass.var);
}
public class First {
public void testB {
ThirdClass ob3 = new ThirdClass();
System.out.println(ThirdClass.var);
ThirdClass.var="CSHARP";
}
public void testC {
ThirdClass ob4 = new ThirdClass();
System.out.println(ThirdClass.var);
}
By webservices calls ( i have a setup) called these methods in secquence testA() --> Display var = "Ruby"
通过 webservices 调用(我有一个设置)调用这些方法在 secquence testA() --> Display var = "Ruby"
testB() --> Display var = "Ruby"
testB() --> 显示变量 = "Ruby"
testC() --> Display var ="CSHARP"
testC() --> 显示变量 ="CSHARP"
So the new changed values were shared by DIFFERENT METHOD CALLS throught application deployment. So scope of ThirdClass was deployment level.
因此,通过应用程序部署,不同方法调用共享了新更改的值。因此,ThirdClass 的范围是部署级别。
回答by babygame0ver
You can Only execute one class at one time from the command line and in your program there are two classes which have main method if you run Second class at one time then it will not execute the main method of the Third class and vice versa. I have tested it on my system.
您一次只能从命令行执行一个类,并且在您的程序中,有两个类具有 main 方法,如果您一次运行 Second class,则它不会执行第三类的 main 方法,反之亦然。我已经在我的系统上测试过了。
回答by Goran Jovic
Static variables are "per class". So it doesn't matter if your static variable in this class has same name/type as some other.
静态变量是“每个类”。因此,此类中的静态变量是否与其他变量具有相同的名称/类型并不重要。
It's global in the sense that no matter how many object of that class you have, if you have them use a static variable, they'll all be using one var.
从某种意义上说,它是全局的,无论您拥有该类的多少个对象,如果您让它们使用静态变量,它们都将使用一个变量。
回答by Jo?o Silvestre
You have two different mains, meaning it will have 2 different execution paths. You either start with one or with the other, the value of the changed static variable is only changed for the current execution, on a different execution it always reset to the default value.
您有两个不同的主电源,这意味着它将有 2 个不同的执行路径。您要么从一个开始,要么从另一个开始,更改的静态变量的值仅在当前执行时更改,在不同的执行中它始终重置为默认值。
Try not to have a second main but rather a static method and then call it on the first main after the change, you will see a different result then.
尽量不要有第二个 main 而是一个静态方法,然后在更改后在第一个 main 上调用它,然后你会看到不同的结果。