• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiler generated method values() for Enum  RSS feed

 
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java and very confused with accessing compiler generated methods in java.

Enum type has a values() method that is generated by the compiler. This is not explicitly listed in javadocs http://docs.oracle.com/javase/1.5.0/docs/api/index.html.

I came upon the value() method while going through a book on java else I wouldn't have know that such a method exists. Which other methods are there that are not listed in javadocs that I can use for enum?

My question is what do developers refer to other than javadocs  for knowing all the methods (implicit and explicit) declared for a particular class or enum while developing a java program.

I know this question is naïve but I am a novice programmer and really need to know this. With all the information out there it is really hard to process and skim for the right information in the right place.



 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not suggest that you the Java 1.5 documentation when starting out, unless you need to support that version of Java. Here is the documentation for 1.8 (current) http://docs.oracle.com/javase/8/docs/api/. Please be aware that Java 1.9 is scheduled to be out before 2018, however it's not expected that projects will start using 1.9 in production right away. I would suspect that it would take at least a few months for adoption to 1.9 become mainstream.

Oracle does have their own line of tutorials that anyone can access for free, which starts here http://docs.oracle.com/javase/tutorial/java/TOC.html and they do have some tutorials/examples with ENUMs including the a sample with the 'values()' method. I would not recommend going through the API documentation as tutorial, but instead use the Oracle trails, that is if you plan on use the Oracle documentation for learning and starting from the beginning.
 
Mona Kha
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then these are the only two locations for looking for info on any classes, enums and interfaces used in java? Thanks
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the official Oracle docs are the true source as they also make the JVM and more. However there are many tutorials and books out there that can be used aside from the official Oracle resources. Not only that you can download the source code for Java from Oracle and "look under the hood" so to speak.
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, the usual system doesn't work very well for enums (nor for arrays). You have to go through the Java® Language Specification where it explains that all enums implicitly extend this class. It also tells you that all enums implicitly have a values() method and a second valueOf() method.
Let's create an enum, with elements, and you can create your own documentation with
javac javadoc MyEnum.java  Somebody pointed out my mistake today: not javac but javadoc.
Now open that HTML file with your browser (or find any randomly chosen enum in the API, maybe this one. At the top of the document it tells you it extends Enum and that extends Object, and also implements Comparable. If you count the methods, you will find this many:-
  • 1: Specified by Comparable: compareTo: cannot be overridden. 1
  • 2: Inherited from Object: final methods which cannot be overridden: wait(×3) notify notifyAll and getClass 6
  • 3: Inherited and overridden from Object: equals clone toString finalize and hashCode: only toString can be overridden again. 5
  • 4: New methods in Enum: ordinal getDeclaringClass and valueOf: cannot be overridden. 3
  • 5: New methods in your enum: valueOf (different from the above) and values: cannot be overridden. 2 So far we have 17.
  • 6: Whichever methods you wrote yourself in the enum; the example I showed you has a third valueOf method written by hand.
  •  
    Saloon Keeper
    Posts: 7993
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mona Kha wrote:Enum type has a values() method that is generated by the compiler. This is not explicitly listed in javadocs http://docs.oracle.com/javase/1.5.0/docs/api/index.html.

    That's because if it did, you would expect to be able to call Enum.values(), which results in a compiler error because Enum doesn't have a values() method.

    I came upon the value() method while going through a book on java else I wouldn't have know that such a method exists. Which other methods are there that are not listed in javadocs that I can use for enum?

    you should make sure to read the class description of types you use regularly. The documentation you linked to explicitly mentions this:
    java.lang.Enum wrote:More information about enums, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.9 of The Java™ Language Specification.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!