#define in Java?

Started by
8 comments, last by markr 19 years, 3 months ago
Does anyone know if there is anything similar to a #define in java? Or maybe an Enum?
Advertisement
Ignore me... [embarrass]

[Edited by - Useless Hacker on January 6, 2005 9:33:15 AM]
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
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
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?
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.
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.
My site Jvitel.com
Alright, I'll use a final int.

Thanks much for your help.
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.
Thanks Doynax, I think i'll just keep it simple and stick with the final then.
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

This topic is closed to new replies.

Advertisement