Dori
Dori

Reputation: 18413

Android version checking and inlining of static final constants

It was my understanding that and static final constants are inlined at compile time when using Java.

Looking at Using the Version-Aware Component - Add the Switching Logic and the use of Build.VERSION.SDK_INT and Build.VERSION_CODES confuses me, as if both these constants values are inlined at compile time this approach would be useless. What am I missing here?

Thanks :)

EDIT: does the fact they are in a static method in an abstract class change this compile-time inlining?

Upvotes: 6

Views: 322

Answers (1)

assylias
assylias

Reputation: 328785

What is inlined is constants that can be determined at compile time, such as:

private final int CONST = 1;

If you check the source code (it's an old version but I suppose it has not changed much), the constants look like this:

public static final String SDK = getString("ro.build.version.sdk");

And this is the getString method:

private static String getString(String property) {
    return SystemProperties.get(property, UNKNOWN);
}

So the constant can't be determined at compile time.

Upvotes: 4

Related Questions