I'm trying to define something in my code, but there is no define in Java.
Asked
Active
Viewed 706 times
-1
Brian Tompsett - 汤莱恩
- 5,438
- 68
- 55
- 126
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 (ie. can not be changed).
So something like:
final int NUMBER_OF_HOURS_IN_A_DAY = 24;
will create a "constant" int with a value of 24
Tularis
- 1,505
- 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
final String key = Value;
e.g.
final String MAX_VALUE = "9000";
and it's better to add static too
static final String MAX_VALUE = "9000";
MSaudi
- 4,012
- 2
- 36
- 61
-
-
-
-
-
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