Java solutions for C-style compiler instructions
•
Java
I have a Java array:
String[] myArray = {"1","2"};
I want to assign different values according to the conditions known at compile time:
String[] myArray = {"A","B","C"};
I'll use something similar in C
#ifdef ABC // ABC stuff here #else // 123 stuff here #endif
But what about in Java?
Solution
class Foo {
class Foo {
static final boolean ABC = true;
public void someMehod() {
if (ABC) { // #ifdef ABC
} else { // #else
} // #endif
}
}
Since ABC is both static and final, the compiler evaluates it at compile time and executes it effectively like a preprocessor
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
