Scope of static variable and methods in Java

后端 未结 6 1367
自闭症患者
自闭症患者 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.

提交回复
热议问题