If I create a class, where will I store all the variables that can be used in different classes?

In static variables, and then import them into classes?

import static Const.*; 

Is it worth doing this?

It turns out that all variables are imported and they will be stored in the memory of each class? Or will only those that will be used in the classroom be imported?

    1 answer 1

    Static variables will always be created, in single copy by definition. This construction simply allows you to avoid having to write Const.SOME_VALUE each time instead of just SOME_VALUE . Since, apart from some confusion (where this constant came from), this does not help much, this construction is not recommended for use.

    • one
      I would not say that this is confusing. The code becomes easier to read and write, and in order to see where the constant came from, there are IDEs. - zzashpaupat
    • @zzashpaupat is a matter of taste and a specific situation, but in most cases the clogging of the global list of names is rather bad than good. - Petr Abdulin
    • My question is whether all static variables will be imported or only those that are used in the class, then when the program is running, all the variables will be stored in memory, even those that are not used, because if I’m in the Const class I’ll store 1000 variables and import all the static ones variables in 100 classes then it turns out that all of them will be stored in memory or something I don’t understand - J Mas
    • @JTan if your "static" class is loaded, then all its static variables will be created, regardless of whether they are used or not. As I wrote above, all static variables will be created in a single copy, your import static Const.*; this is nothing more than the convenience of accessing static class variables (as when using "normal" imports). - Petr Abdulin
    • it turns out they will be stored in a single copy and not in each class where I imported? and if I change the field of this class from one of the classes, it will change in another class. - J Mas