Solved

Mimicking preprocessor

Posted on 2002-03-13
3
224 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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

735 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