Timeline for Best practice for Constant Class in Java
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 10, 2018 at 10:00 | history | edited | Suyash | CC BY-SA 4.0 |
Improved formatting.
|
Jul 9, 2018 at 6:49 | history | edited | Billal BEGUERADJ | CC BY-SA 4.0 |
deleted 125 characters in body
|
May 23, 2017 at 11:33 | history | edited | Community Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Apr 19, 2016 at 7:46 | vote | accept | Muneeb Mirza | ||
Apr 18, 2016 at 9:50 | comment | added | Suyash | Do you have some strong reasons to use constants? If no, go with enum without hesitation! | |
Apr 18, 2016 at 8:47 | comment | added | Muneeb Mirza | i am confused, should i use enums or constant strings for variables ranging hundreds with types including primitive and custom built? What say you? | |
Apr 18, 2016 at 8:11 | comment | added | Suyash | It depends on implementation. toString can be used if you want to display different contents from what is present in given enum, otherwise you can use valueOf() or name() which displays contents which are actually present in enum. For ex, in case of enum PLUS("+"), if you use PLUS.valueOf or PLUS.name then you can only get "+" as an output but if you want some custom display then you should use overridden toString(). For many enums, you could always create a map from string to value statically so you only need to map it once, assuming that the returned string remains the same over time. | |
Apr 18, 2016 at 5:45 | comment | added | Muneeb Mirza | gorbeia.wordpress.com/2015/03/11/java-enums-vs-constants in this link, the last code chunk shows custom enum class with functions that I can use for strings. Is it effecient to have a class that hold hundreds of enums and run function for each single of them? | |
Apr 11, 2016 at 19:46 | history | migrated | from stackoverflow.com (revisions) | ||
Apr 11, 2016 at 4:37 | comment | added | Suyash | Yes that's true, enum does take more memory than constants but with benefits. Following statement will help to put light on it : All enum objects are singletons. This is guaranteed by Java language – you can create an instance of enum only by its definition. This means that you pay for enum object once and then you only spend 4 bytes per its reference (in this article I will assume that object references occupy 4 bytes). Refer following URL for more details on enum : java-performance.info/memory-consumption-of-java-data-types-1 | |
Apr 10, 2016 at 10:42 | comment | added | Muneeb Mirza | I have heard enums take lots of memory space. is this ok to use that many ENUMS? | |
Apr 8, 2016 at 11:23 | comment | added | Steffen Harbich | I agree that enums should be used instead of constants where possible, for type-safety. | |
Apr 8, 2016 at 6:37 | history | answered | Suyash | CC BY-SA 3.0 |