Jump to content
  • Advertisement
Sign in to follow this  
Kojo

#define in Java?

This topic is 5067 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know if there is anything similar to a #define in java? Or maybe an Enum?

Share this post


Link to post
Share on other sites
Advertisement
The Java language itself has no #define as such. The nearest thing to it is:
public static final type name=value;
which must be in a class definition. This does incur an overhead though so it's not that useful in limited capability systems (e.g. phones).

There is, however, no reason why you can't use #define and run the java code through the C preprocessor only before compiling. Most Java IDEs don't allow this so you may need to use a scripted build system (make, ant, etc...). This is what I do for developing multiple versions of my mobile phone games.

Skizz

Share this post


Link to post
Share on other sites
Thank you for your reply. However, I'm looking for a way to assign a word to a value. In C[pp] what I would do would be...


#define left 0
#define right 1
#define down 3
#define up 4

or

enum direction {left, right, up, down};
//this may be wrong.. haven't used enums for quite a while



Any way of doing somthing like this?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kojo
Does anyone know if there is anything similar to a #define in java? Or maybe an Enum?

Java 1.5 has enums.
If you really need macros you could always run your source files through a preprocessor. C's for example, or perhaps something a bit more powerful.

Share this post


Link to post
Share on other sites
mmm, if you are using your #define as constants you can make a constant variable in java using the word final:

C++

#define MAX 100



Java

final int MAX = 100;



something similar for enums, just that in Java you have to make a new class with a bunch of static constant variables:

C++

enum Colors {
BLUE=0,
YELLOW,
RED,
GREEN,
WHITE,
BLACK
};



Java

public class Colors {
public static final int BLUE = 0;
public static final int YELLOW = 1;
public static final int RED = 2;
public static final int GREEN = 3;
public static final int WHITE = 4;
public static final int BLACK = 5;
};



and you access the values just typing Colors.BLUE for example.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kojo
How would I run the code through the C preprocessor?

Just pipe your source files through the preprocessor (probably named cpp) before feeding them to the compiler. This is easy to do with makefiles, otherwise it depends on what development environment you're using.
However I'd advice against using them unless you really need them. This might be the right solution for a few situations however for the most part you're probably just obfuscating your code for other java programmers.

Share this post


Link to post
Share on other sites
Thanks Doynax, I think i'll just keep it simple and stick with the final then.

Share this post


Link to post
Share on other sites
I think that if you declare constants in a class (with static final), some obfuscators will optimise them out completely. Proguard may do this.

This is of course only remotely important on J2ME, even then it's not THAT important.

J2ME is a tight environment, but it's not SO tight that a few extra bytes for a constant will hurt much (unless you have FAR too many constants).

Plus any constants you define and don't use will get thrown away during obfuscation anyway.

Create a whole extra class just for constants if you like - it will still make your .jar no bigger as it gets optimised away into nothing.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!