Solved

Mimicking preprocessor

Posted on 2002-03-13
3
220 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now