Results 1 to 5 of 5
Thread: Preprocessor definitions
- 04-22-2011, 06:12 AM #1
Preprocessor definitions
Hey, I'm an avid C/C++ programmer, and I love my "#define" preprocessor macro. But I heard that java doesn't have anything like it?
Anyone know a work-around?Good with: C/C++, DarkGDK, PHP, MySQL
Current reading: The Linux Programming Interface
- 04-22-2011, 06:15 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't know preprocessor macro's too well in c/c++, but what are you looking to simulate with a java-like macro?
Others with better c++ knowledge however; may be able to give much better advice.
- 04-22-2011, 03:48 PM #3
I'm looking to create some sort of global variables essentially.
A quick explanation of how #define works is that you have it in your code, like this:
What the preprocessor actually does is search all your code for the #define, and replaces it, so the example above, when shipped off to the compiler, is actually:Java Code:#define MEANING_TO_LIFE 42 // Later on in your code if (21 * 2 == MEANING_TO_LIFE) { // More code here
As I said, I'm just looking for a method of having some global variables throughout my whole java program.Java Code:// Later on in your code if (21 * 2 == 42) { // More code hereGood with: C/C++, DarkGDK, PHP, MySQL
Current reading: The Linux Programming Interface
- 04-22-2011, 04:21 PM #4
In Java there isn't such a beast as a global variable. I think what you're looking for is static final variables, which the compiler treats as compile-time constants and substitutes the value wherever the constant is used. Oversimplified example:
When compiling class B, the constant value "Class A" will be substituted in place of A.NAME.Java Code:public class A { static final String NAME = "Class A"; } public class B { public B(String name) { if (name.contains(A.NAME)) { System.out.println("parameter to constructor contains " + A.NAME); } } }
Note that any subsequent change to the value of the constant in the code of class A will not be propagated to class B until both A and B are recompiled.
db
- 04-22-2011, 07:50 PM #5
Okay, thank you, that helps a lot.
Good with: C/C++, DarkGDK, PHP, MySQL
Current reading: The Linux Programming Interface
Similar Threads
-
Ant problem:Could not load definitions from resource org/apache/tools/ant/antlib.xml
By jayyu317 in forum New To JavaReplies: 0Last Post: 03-22-2010, 02:58 AM -
javax.servlet.ServletException:Can't get definitions factory
By Peter in forum JavaServer Faces (JSF)Replies: 3Last Post: 12-02-2008, 07:29 AM -
How to use Inner bean definitions via nested bean elements
By Java Tip in forum Java TipReplies: 0Last Post: 03-30-2008, 10:03 AM -
How to use Inner bean definitions via nested bean elements
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks