Enumerations in java compilation

I am trying to learn java from the bottom up, and I got this wonderful book to read http://www.amazon.com/o/ASIN/0071591060/ca0cc-20 . Now I found an example in a book about declaring Enums inside a class, but outside of any methods, so I gave it a snapshot:

Enum CoffeeSize { BIG, HUGE, OVERWHELMING }; 

The book says enum and I get this compilation message Syntax error, insert ";" to complete BlockStatements Syntax error, insert ";" to complete BlockStatements

Are transfers important at all? I mean, if I miss this or its possible, what will I use them someday?

+4
source share
3 answers

This is the correct way to declare enum inside a class:

 public class Main { enum Foo { One, Two, Three } public static void main(String args[]) { System.out.println(Foo.One); } } 

You are using the enum keyword, not enum , which is the superclass of all enum instances.

+2
source

As with Java 5, enum is a keyword, which is why capitalization is important.

As for whether you need to know them or not, it really is up to you. They were not part of the language for several versions, but they are really nice. Quote from the same book:

... you can guarantee that the compiler stops you from assigning anything to CoffeSize other than BIG , HUGE or OVERWHELMING .

Some people do not care about this, but I personally would like the compiler to do all the work that I can get.

(In addition, Java Tutorials are another great source for learning the basics.)

+1
source

Remove the ';'. You declare Enum as follows.

 enum CoffeeSize { BIG, HUGE, OVERWHELMING } 

For more information, visit here.

0
source

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


All Articles