Global variables in Java

后端 未结 24 2231
青春惊慌失措
青春惊慌失措 2020-11-22 11:56

How do you define Global variables in Java ?

24条回答
  •  忘掉有多难
    2020-11-22 12:37

    There is no global variable in Java

    Nevertheless, what we do have is a static keyword and that is all we need. Nothing exists outside of class in Java. The static keyword represents a class variable that, contrary to instance variable, only has one copy and that transcends across all the instances of that class created, which means that its value can be changed and accessed across all instances at any point.

    If you need a global variable which can be accessed beyond scopes, then this is the variable that you need, but its scope exists only where the class is, and that will be all.

提交回复
热议问题