I'm trying to define something in my code, but there isn't any define
in Java.
Asked
Active
Viewed 729 times
-1

Peter Mortensen
- 30,738
- 21
- 105
- 131

MCPEBukkit team
- 1
- 2
2 Answers
1
Java as such does not exactly "have" constants (which is what you define using the define() function in PHP). Rather, you can make the variable final. This way, it behaves like a constant value (i.e., can not be changed).
So something like:
final int NUMBER_OF_HOURS_IN_A_DAY = 24;
will create a "constant" integer with a value of 24.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Tularis
- 1,506
- 1
- 8
- 17
-
http://stackoverflow.com/questions/12517978/java-constant-variable-examples Also has a good answer to this. – Tularis Feb 03 '14 at 01:48
-
Even better if you make it static too. – Dawood ibn Kareem Feb 03 '14 at 01:49
-
1That's true; but it really depends on the context. Could also make it public ;) – Tularis Feb 03 '14 at 01:51
1
Use:
final String key = Value;
For example,
final String MAX_VALUE = "9000";
And it's better to add static too:
static final String MAX_VALUE = "9000";

Peter Mortensen
- 30,738
- 21
- 105
- 131

MSaudi
- 4,442
- 2
- 40
- 65
-
-
-
-
-
What do you mean by multiple strings = 9000 ? You can make many constants as you want : static final String MAX_VALUE = "9000"; static final String MAX_VALUE2 = "9000"; static final String MAX_VALUE3 = "9000"; – MSaudi Feb 03 '14 at 07:13