A
A
Arti-Jack2016-11-09 21:03:08
Java
Arti-Jack, 2016-11-09 21:03:08

Is it possible to pass arguments to the enum constructor in this way?

Today in a Java SE class I ran into a small problem. I came across a syntax that was incomprehensible to me while reading interesting information about Enums on the site .
Let's start with what I already know:

  1. I know what an Enum is
  2. I also know that all variables in an Enum are declared static final by default .
  3. I know that using the method values()is very convenient to iterate them
  4. I also know that Enum has the ability to override methods using anonymous classes.
    For example:
    public enum Books {
    
        DARK_TOWER {
            @Override
            public void info() {
                System.out.println("Dark Tower, autor - Stephen King");
            }
        },
    
        KTULHU {
            @Override
            public void info() {
                System.out.println("Ktulhu, autor - Govard Lovecraft");
            }
        };
    
        public abstract void info();
    
    }


(If I'm wrong in any of the above, then please let me know)
But there are a couple of things that I just can not understand.
First, this is the syntax:
enum Type { 
    INT(true) { 
        public Object parse(String string) { return Integer.valueOf(string); } 
    }, 
    INTEGER(false) { 
        public Object parse(String string) { return Integer.valueOf(string); } 
    }, 
    STRING(false) { 
        public Object parse(String string) { return string; } 
    }; 
 
    boolean primitive; 
    Type(boolean primitive) { this.primitive = primitive; } 
 
    public boolean isPrimitive() { return primitive; } 
    public abstract Object parse(String string); 
}

What do the values ​​mean in true the falseconstructor(?) of Enum?
Secondly, let's say somewhere in main I have this code:
Books b = Books.DARK_TOWER;
        switch (b) {
            case DARK_TOWER:
                Books.DARK_TOWER.info();
                break;
            case KTULHU:
                Books.KTULHU.info();
                break;
        }

I just can't figure out why in case`e I can't write like this: case Books.KTULHU: ...?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evhen, 2016-11-10
@Arti-Jack

You can consider this example:

public enum A {
    AAA {
        public void i() {
            System.out.println("Hi AAA");
        }
    },

    BBB {        
        public void j() {
            System.out.println("Hi BBB");
        }
    },
    CCC;

    public static void main(String[] args) {
        System.out.println(A.AAA.getClass().getName());
        System.out.println(A.BBB.getClass().getName());
        System.out.println(A.CCC.getClass().getName());
    }
}

We get:
com.home.A$1 - anonymous class
com.home.A$2 - anonymous class
com.home.A
i.e. 3 *.class files created
Ultimately, class A itself is final and cannot be extended, but in order for enums to implement abstract methods, interfaces, and as in the example above, it can be assumed that inside the JVM it is allowed to inherit from enum.
After decompiling the class "A$1.class" with javadecompilers , we get code that confirms the hunch about anonymous classes
static final class A
extends com.home.A {
    A(String string2, int n2) {
        super(string, n, null);
    }

    public void i() {
        System.out.println("Hi AAA");
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question