Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    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:
    Java Code:
    #define MEANING_TO_LIFE 42
    
    // Later on in your code
    if (21 * 2 == MEANING_TO_LIFE) {
        // More code here
    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:
    // Later on in your code
    if (21 * 2 == 42) {
        // More code here
    As I said, I'm just looking for a method of having some global variables throughout my whole java program.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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:
    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);
        }
      }
    }
    When compiling class B, the constant value "Class A" will be substituted in place of 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

  5. #5
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Okay, thank you, that helps a lot.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

Similar Threads

  1. Replies: 0
    Last Post: 03-22-2010, 03:58 AM
  2. javax.servlet.ServletException:Can't get definitions factory
    By Peter in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 12-02-2008, 08:29 AM
  3. Replies: 0
    Last Post: 03-30-2008, 11:03 AM
  4. Replies: 0
    Last Post: 09-26-2007, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •