简体   繁体   中英

Is “final static variables ” efficient in Java (Android)?

I'm developing an app in Android. I use a lot "final static" variables to define my constants. But I'm very stric with the memory used by my application.

Maybe I have 200 constants (int, string, double, ...). It is much better to program with constant variables that use numbers. But, how efficient is this?

Using CI can use #define , and when I put:

#define constant 10
int var2 = constant;
int var3 = constant;

The compiler translates the code to:

int var2 = 10;
int var3 = 10;

But using Java, I think that all these variables stay in memory. There is something so efficient as #define for java?

If you want to use something similar to C's ifdef you should do something like:

final static boolean COMPILE_THIS = false;

This will cause the following code not to be part of your program ("compiled"):

if (COMPILE_THIS) {
   printToScreen("HELLO");
}

Google uses this technique a lot in the Android code when they don't want to "compile" parts of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM