Results 1 to 4 of 4
  1. #1
    renukayarkar is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Using a C enum in Java

    Hi All,

    I am trying to use an enum from C in my java code. Is there a way to do it except definining ints in Java? We already have a class with static fields for the C-enum.

    For example, we have a following enum in C-

    C enum:
    typedef enum {
    value1,
    value 2
    }example1;

    In Java, we are doing following,

    public class example1 {
    static public int value1 = 0;
    static public int value2 = 1;
    }

    However, I want to be flexible to use an enum directly from C-side so that if there are any changes there , I dont have to manually make a change in Java. For example, if there is a new enum value added in C-enum as follows-

    typedef enum {
    value1,
    value1_2,
    value 2
    }example1;

    I will have to modify the Java enum too to match this one.

    Please let me know if anyone has any ideas. I am trying to see if there is any way we can do this in JNI. Any other suggestions are most welcome.

    Regards,
    Renu

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Google JNI

    However, if you are modifying the "c-side" of the application I see no reason why the "Java-side" can't also be changed. An enum is not especially anything difficult.

    Then again, depending on what is being done with the enum you are almost guaranteed of needing to make other changes in the code anyway.

  3. #3
    renukayarkar is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    hi Masijade,

    Thanks for your really quick reply. I really appreciate it.

    The real issue is that we want our java code to be compatible with 2 different versions of software (which is in C). The later version has the same enum with one additional item in it. So, if we make that change in the enum, it will break the compatibility with the previous version (which has older enum). Hence, we are trying to use the enum directly so we dont have to worry about breaking it. I know we can have a separate enum for both the versions and use it depending on what version is selected. But, that will require us to make changes in all the java files that are using this enum currently.

    If getting an enum from C-side to Java is not possible, is there a way we can define an enum on java side depending on some feature? We are using #ifdef in C to pick up the correct version. But, I did not see anything like that in Java. We can set a final variable to true or false and use that to differentiate but that will work only runtime. Is it possible in java to conditionally compile a particular code (enum definition) i.e. define one version of enum if a certain feature is enabled and other if the feature disabled?

    In C it can be shown as -

    #ifdef myFeatureEnabled
    typedef enum {
    value1,
    value 2
    }example1;
    #else
    typedef enum {
    value1,
    value1_2,
    value 2
    }example1;
    #endif


    Is there a way to do similar in Java? Again, any ideas are welcome and appreciated.

    Regards,
    Renu

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Those are preprocessing commands, you can use a preprocessor with Java, as well. It does, of course, mean two different versions of the "binary".

    The other question is, is it even necessary to use an enum in Java? Probably not.

Similar Threads

  1. Replies: 1
    Last Post: 10-13-2009, 08:31 AM
  2. Replies: 1
    Last Post: 05-31-2009, 11:57 PM
  3. Replies: 1
    Last Post: 05-31-2009, 03:17 PM
  4. Replies: 16
    Last Post: 05-28-2009, 04:41 PM
  5. Replies: 24
    Last Post: 05-18-2009, 11:23 AM

Tags for this Thread

Posting Permissions

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