Java: Best approach to having a long list of variables needed all the time without consuming memory?

I wrote an abstract class to contain all the rules of the application, because I need them almost everywhere in my application. So most of them contain variables static final, something like this:

public abstract class appRules
{
    public static final boolean IS_DEV = true;
    public static final String CLOCK_SHORT_TIME_FORMAT = "something";
    public static final String CLOCK_SHORT_DATE_FORMAT = "something else";
    public static final String CLOCK_FULL_FORMAT = "other thing";
    public static final int USERNAME_MIN = 5;
    public static final int USERNAME_MAX = 16;
    // etc.
}

The class is large and contains LOTS of such variables.

My question is:

  • Doesn't set static variables do these variables float in memory all the time?
  • You suggest instead of having an abstract class, I have an instance of a class with non-stationary variables (simple public final), so I only instantiate the class and use the variables only when I need them.
  • Or what I'm doing is a completely wrong approach, and are you suggesting something else?
+3
6

?

. , . , , , .

, ( ), , .

. . JVM String, , , () , .

, .

- ?

.


@Peter Lawrey , , . :

. , , . , .

+4

, RAM .., ( ), - , , .

, , : .

, .

, , , . : . - ( , ), , , .

, , , .. :

private static final String CONSTANT_FOO;
private static final String CONSTANT_BAR;
static{
    try{
        Properties props = new Properties();
        InputStream is = 
            MyConstantClass.class
            .getResourceAsStream("my.module.properties");
        props.load(is);

        // you'll actually want to move this to finally, but I'm lazy
        is.close();

        CONSTANT_FOO = props.get("constants.foo");
        CONSTANT_BAR = props.get("constants.bar");

    }catch(Exception e){
        throw new IllegalStateException(e);
    }
}

, , .

+7

. . .

, , . - , .

, .

+3

, . , , 3000 rules, - String of 1000 characters. 3MB. ?

, , 30 000 . . , - .

+2

, . , lazy-initialize . java.util.prefs.Preferences .

, RuleUtils appRules , .

0

, . ? 24 1800 . 75 GB, 8 .

50 , 1 , 10 .

With a minimum wage, you should not spend 1 minute saving 1 MB on a PC or server.

0
source

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


All Articles