I have a very specific problem about the method java.lang.Enum.values().
I would like to override its javadoc. Very precisely, the current javadoc for this is, after I created my own enum:
public static MyClass.MyEnum[] values()
...
This method may be used to iterate over the constants as follows:
for (MyClass.MyEnum c : MyClass.MyEnum.values())
System.out.println(c);
Returns:
...
But in my company System.out
calls are considered bad practice so I would like it not to be shown. My first try was to override values()
but it is apparently not possible. Is there another way I can do this? Or is the only possibility to update the generated doc ?
I am also curious about why values()
is not overridable. I read on other questions that "it is generated by the compiler". But can someone be more precise? It seems that it's generated from the enum's name, but it does not explain why.
Since you cannot override valueOf method you have to define a custom method ( getEnum in the sample code below) which returns the value that you need and change your client to use this method instead. getEnum could be shortened if you do the following: v. getValue().
The valueOf() method returns the value of given constant enum.
You can use the "new" keyword to override the field you defined in the base class. I used this, and it says it hides the inherited member.
By default, the enum value is its method name. You can however override it, for example if you want to store enums as integers in a database, instead of using their method name.
values
is a static method and is not subject to overriding. You cannot provide your own method to replace the generated one, and this is by specification.
There is no standard mechanism to replace the Javadoc of a method whose source code you don't control, but you could probably mess around with either the build tool, or, if all else fails, the final Javadoc HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With