I have some doubts about usage of static method in Java. I read many places static variables are instance independent so be comes global.
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!!!
Also do is creating static variable resource intensive compared to instance variable?
Please suggest.
It means the static variable are NOT global variable but only global to single execution!!!
Of course they are. All variables that are not persisted to some kind of storage (like the hard disk) do not retain their values between distinct executions of the program.
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.
The term global has nothing to do with the variables persistence, and scope is defined only within the running program.
@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.
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"
testB() --> Display var = "Ruby"
testC() --> Display var ="CSHARP"
So the new changed values were shared by DIFFERENT METHOD CALLS throught application deployment. So scope of ThirdClass was deployment level.
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.
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.
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.
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.
精彩评论