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 ?

2
  • 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. Commented Aug 26, 2015 at 21:05
  • @morgano I suppose this is the case, thanks for your comment. Commented Aug 26, 2015 at 21:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
#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.
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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.