0

I am writing a cross platform game in IntelliJ IDEA using Java and I have run into a situation where I can't seem to find this feature which Visual Studio had:

1

This feature allowed me to have a condition, (2 lines of code), for example #if keyword1/#endif, and the code between those 2 lines, compiled only when the current project had keyword1 declared as compilation symbol through the project settings.

Is there any similar feature in IntelliJ IDEA ?

dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 1
    Two things: 1 - this is NOT a duplicate (this question asks if IntelliJ has a "conditional inclusion" like Visual Studio, it DOESN'T ask if one can do conditional compilation in Java). And 2 - No, IntelliJ doesn't have such feature, that would produce code very dependant on the IDE, contrary to Java's philosophy. – morgano Aug 26 '15 at 21:05
  • @morgano I suppose this is the case, thanks for your comment. – dimitris93 Aug 26 '15 at 21:08

1 Answers1

1

This is a preprocessor symbol, nothing IDE-dependant. There are some workarounds for this in java. For example you could use something like this:

public static final boolean DEBUG = false;

public void someMethod(){
    if(DEBUG)
        dosomething();
    else
        dosomethingElse();
}

Most precompilers will optimize this such that the result won't contain the if-else statement and reduce this to a simple call to dosomethingElse. But there's no preprocessor/precompiler statement like in c++ for java.

  • The VS feature was completely IDE-dependant. What you suggest is very different from the feature of Visual Studio. For example, what if I wanted to have 2 different functions or classes or variables, with the same name, one for android and one for desktop. The code was being *ignored by the compiler*, just like a comment would, if It was inside `#if keyword`/`#endif` and your project didn't contain the `keyword` as a compilation symbol. – dimitris93 Aug 26 '15 at 20:54
  • `#if` is a preprocessor statement, which is language-dependant. VS only provides a UI to simplify this kind of behaviour. And as i stated in my answer, java doesn't provide preprocessor statements and this would just be a workaround that **is likely** to result in the same behaviour. –  Aug 26 '15 at 21:02
  • Oh, I once have heard someone saying that the `#if`/`#endif` is VS dependent, but I guess they were wrong. However, even if java doesn't support this feature as a language, I don't see how this feature has not been added in IntelliJ IDEA, to work internally, only inside the IDE. I suppose there are other complications ? Hmm... Nevertheless, your answer seems accurate. Thank you. – dimitris93 Aug 26 '15 at 21:08
  • 1
    there are actually preprocessors for java, but this is not a default-feature of java, and so far there's no plugin for intellij. though it would be quite usefull. if you want a pure java approach the way i showed in my answer is the only one –  Aug 26 '15 at 21:11