Variables.class in java

So, I know how to refer to variables from different classes, but I'm wondering if I should create Vars.class to host all of my global vars.

This would make it easier for me if I need to change them later, but I wonder how bad this idea is? Are more var references needed from another class?

thanks

EDIT: When I say "global variables", I meant "global constants." Apologies for the confusion. I want them to belong to other classes, but they will not be changed on the fly.

Vars.java

package test; public class Vars { static int variable1 = 16; static int variable2 = 32; } 

Start.java

 package test; public class Start { public Start() { int i = Vars.variable1; } } 
+4
source share
5 answers

So, I know how to refer to variables from different classes, but I'm wondering if I should create Vars.class to host all of my global vars.

No. First of all, you should try to avoid global variables. Variables should represent the state of a particular object - so group variables are in accordance with reasonable boundaries for useful objects, and then make sure that each object is aware of any other objects in which it should depend.

It makes little sense to use an object-oriented language if you are going to throw away object orientation.

Taking the time to develop the appropriate types in your application will no doubt be slower in a very short time than just having global variables ... but it will make your code much easier to maintain and test.

As for performance: you are too worried about this too soon. Access time for variables will almost never be a significant effect. Create your code in the easiest way. Define performance criteria and eligibility criteria. See if your code meets these criteria. If it is not, find out why it is not, and make the cleanest change that you can improve to work (all the time).

In addition, the variables themselves should almost always be private: storage is a solution for implementation; consider how and how to expose state through methods.

+7
source

Global variables are a bad idea. They have many articles on the Internet. This one is good

If you want to handle global constants

eg,

 public static final Integer MY_CONSTANT = 1; 

then I would still put the constant in the class to which it refers. I personally would not create a class of variables. Try to keep the code encapsulated so that the variables are as close to them as possible.

+3
source

Answer: No. If you think about security, then there are no functional flaws.

+2
source

Global variables are a bad idea.

This would make it easier for me if I need to change them later however however

Maybe you need constants? It makes sense to have all the constants in one place if you think you need to change them. Use the public static final constant for constants:

 package test; public class Constants { public static final double EPSILON = 0.001d; } package test; public class Start { public Start() { ... if(arg1 - arg2 < Constants.EPSILON) ... } } 
0
source

I prefer to keep constants close to the class or classes that use them. No catch objects; no interfaces.

If objects or values ​​are required for a large number of objects, I would consider introducing them using the DI factory.

If the values ​​change, I think about putting them into the database and accessing them as a configuration.

Nothing but what you suggested.

0
source

Source: https://habr.com/ru/post/1436591/


All Articles