• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

[Performance] Enums vs Constants

ron321

Lurker
Hi all,

Please help me this situation, if i have codes like that:
[HIGH]
public void writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("name", "Jack Hack");
object.put("score", new Integer(200));
object.put("current", new Double(152.32));
object.put("nickname", "Hacker"); } catch (JSONException e) {
e.printStackTrace();
}
System.out.println(object); } [/HIGH]
I don't want hardcode such as "name, score, current, nickname".
I would like to use enums or constants.
With enum:
[HIGH]
public enum PersonEnum {
Name("name"),
Score("score"),
Current("current"),
Nickname("nickname");
private String value
PersonEnum (String value) {
this.value = value;
}
public String getValue(){
return value;
}
}
[/HIGH]
With constant:
[HIGH]
public static final String Name = "name"; ........etc.
[/HIGH]

So, what way should I use if i have many json objects similar above?
How about the performance ? I appreciate any advice on the problem.


​


​
 
As I understand, you should use an enum if you want to mostly return all the fields together, and constants if you mostly want to use them individually.
from a performance viewpoint, constants are probably more memory and CPU efficient. I'd be very interested in what others think.
 
Back
Top Bottom