I study java. I got to the transfers, and then just the removal of the brain is going on. If I write something incorrectly (and then I write as I myself understood everything), then correct and explain, please.

enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE; } 

In this line, the compiler created 4 instances of the Size class, which contain the names of enumeration constants and their sequence numbers using some Size class constructor, which is inherited from this superclass constructor:

 protected Enum(String name, int ordinal); 

I cannot create somewhere else of the Size class instance, since There is no constructor with the appropriate access key. Suppose there is still more or less clear. I went further:

 package main; enum Size { SMALL(10), MEDIUM(20), LARGE(30), EXTRA_LARGE(40); private int size; Size(int size) { //super("Name", 1); this.size = size; } int getSize() { return size; } } public class Main { public static void main(String... args) { Size size = Size.SMALL; //Size size1 = new Size(3); } } 

Then it is not clear: Why the compiler does not skip super ("Name", 1) in the constructor of the Size class. After all, the constructor I invoke in the Enum superclass is declared as protected. In addition, it is not the default constructor and it is the only one in the Enum superclass. Those. I MUST call him explicitly !!!

And it is not clear:

The compiler does not skip Size size1 = new Size (3), although I am in the same package!

    1 answer 1

    You climb a little deep. The enum keyword is a kind of syntactic sugar.

    Why the compiler does not skip super ("Name", 1) in the constructor of the Size class.

    Because, you declare Size not as class Size extends Enum , but as enum Size . From the point of view of type checking, there is no explicit inheritance here and no one will give you access to the Enum(String name, int ordinal) parent constructor Enum(String name, int ordinal) .

    Those. I MUST call him explicitly !!!

    Why so much expression). Not required, because you do not inherit anything.

    The compiler does not skip. Size size1 = new Size (3)

    Of course it does not. You cannot create enum instances through new . All the necessary instances are declared in the enum-e itself, and that’s fine.