Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mimicking preprocessor

Posted on 2002-03-13
3
Medium Priority
?
228 Views
Last Modified: 2011-09-20
New to Java.  I am told that Java does not have the concept of a preprocessor like other languages such as C/C++.  How do you perform conditional compilations in Java?  What I want to be able to do is have certain code executable in release or system test code but not in unit test code.

0
Comment
Question by:c095276
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 total points
ID: 6860893
You're right, there is no preprocessor in Java. In fact, there isn't one that is standard with any language, except C/C++.

The Java solution to conditional compilation is to use constants. The constant can be set, before compilation, to control what happens. For instance:

public static final boolean DEBUG  =false;

if(DEBUG)
{   System.out.println("Stuff");
}



This will wind up being conditional compilation (rather than just an if statement) because any Java compiler will omit compiling this code fragment if DEBUG is false, since it is easy to see that it will never be executed. To "turn it on" you change the value of DEBUG to true before your next compile.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6860901
java 1.4 is the first try to differentiate the release and debug code. In all other releases you have same code for release and debug purposes. If you want to add some code, that must not be executed in the release, you can do this by using a real variables. For example:
class MyClass {
public static final boolean DEBUG = true;
...
public static void main(String[] arg) {
...
if (DEBUG) {
  // do something
}
...
}
}

because the DEBUG is public and static - it is global and you can access it from all classes using this code:
MiClass.DEBUG
0
 
LVL 16

Expert Comment

by:imladris
ID: 6869166
Did that information help?

If so then it is now time to grade the answer. If not, perhaps a clarifying question would be useful.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question