Scope of static variable and methods in Java

后端 未结 6 1270
自闭症患者
自闭症患者 2021-01-18 21:38

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 ThirdC         


        
相关标签:
6条回答
  • 2021-01-18 21:52

    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.

    0 讨论(0)
  • 2021-01-18 21:52

    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.

    0 讨论(0)
  • 2021-01-18 21:56

    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.

    0 讨论(0)
  • 2021-01-18 22:02

    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.

    0 讨论(0)
  • 2021-01-18 22:03

    @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.

    0 讨论(0)
  • 2021-01-18 22:11

    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.

    0 讨论(0)
提交回复
热议问题