Is this ENUM stream method safe?

is this safe ENUM code stream?

enum One{ IONE; public mone(){ // some code } } 

when is the code above not safe?

+4
source share
3 answers

The enum value is guaranteed to only be initialized once, ever by one thread, before its use. However, the methods you add to the enum class do not offer any guarantee of thread safety. If you have an enumeration so that methods do not change its state, then by definition they are thread safe

+4
source

This is until your method itself.

The listings are specifically listed:

  • they are initialized when the class is loaded (so you can use them in the annotation);
  • they are always final ;
  • enum values ​​are always static final .
+1
source

There is no general data in the One list, and all the variables in mone (which require a return type) are local. So it all depends on the content of mone . If the method uses singleton with state in other classes, you may have problems.

0
source

Source: https://habr.com/ru/post/1486416/


All Articles