Using global variables

Why does this happen when I read the code of others, I often see the widespread use of "global variables"?

For example, in Java code:

public class SomeClass {
   Button btn1;
   private void someMethod() {
       btn = new Button();
   }
}

btn1is declared as a “global” and “convenient” variable to be used as easy access throughout the class. But when there is no modifier on it, it uses the default access in Java by default.

Could this be a security risk? Why don't people declare them using a private modifier right away if they plan to use them in only one particular class?

+5
source share
6 answers
  • btn1 is not a global variable. this is a class instance variable
  • , "", .. btn1 , , SomeClass
  • SomeClass - ( ..), .
  • , .
+3

( Java? , ). - .

- private-package, , .

"" , , .

+5

. , , , , SomeClass. Java . - , JVM, .

, , - , , . ( , , .) , , , API .

+3

private/public , / .

, , : , , .

+1

Java , "global". , , . , .

, ?

* / //, +

* + -

+1

.

: .

+1

All Articles