Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use an enum copy constructor in Java?

I'm trying to complete a project, and though I have tried, I can't seem to do this. Here's the enum:

public enum Symbols {
    /**
     * The Seven
     */
    SEVEN(12,"images/seven.jpg"),
    /**
     * The Watermelon
     */
    WATERMELON(10,"images/watermelon.jpg"),
    /**
     * The Orange
     */
    ORANGE(8,"images/orange.jpg"),
    /**
     * The Plum
     */
    PLUM(6,"images/plum.jpg"),
    /**
     * The Lemon
     */
    LEMON(4,"images/lemon.jpg"),
    /**
     * The Cherry
     */
    CHERRY(2,"images/cherry.jpg");

    private int payout;             // Symbol payout value
    private BufferedImage image;    // Symbol image
    private String icon;            // Symbol file name

    /** Constructor - Payout must be positive and even, file name must not be null
     * @param payout - Symbol payout amount
     * @param icon - Symbol image file name
     */
    Symbols(int payout, String icon){
        this.payout = payout;
        this.icon = icon;
        loadImage(icon);
    }

    /** Copy Constructor - Symbol must not be null
     * @param s - A single symbol
     */
    Symbols(Symbols s){
        payout = s.payout;
        icon = s.icon;
        loadImage(icon);
    }

Now I can go into main and create a symbol named "s" like this:

Symbols s = Symbols.CHERRY;

But I'm having trouble making a copy of "s" using the copy constructor given:

Symbols t = Symbols(s);

If I try to do this, I get the error: "The method Symbols(Symbols) is undefined for the type Symbols."

Thanks

like image 432
LTH Avatar asked Oct 26 '11 03:10

LTH


People also ask

Can we use enum in constructor Java?

Example: enum Constructor The constructor takes a string value as a parameter and assigns value to the variable pizzaSize . Since the constructor is private , we cannot access it from outside the class. However, we can use enum constants to call the constructor.

Can we clone enum in Java?

The java. lang. Enum. clone() method guarantees that enums are never cloned, which is necessary to preserve their "singleton" status.

Can enum contain constructor?

enum and constructor:enum can contain a constructor and it is executed separately for each enum constant at the time of enum class loading. We can't create enum objects explicitly and hence we can't invoke enum constructor directly.

Can enum type define constructor?

The enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor. The enum constructor must be private . You cannot use public or protected constructors for a Java enum .


1 Answers

Well you need to make it public but it still isn't going to work. There are no copy constructors in Java; you can't 'new' an Enum, which is the next error; and in any case Java goes to great lengths to ensure there is only one copy of each enum value per JVM. What exactly do you think you need a copy constructor for?

Solution:

Symbols s = Symbols.CHERRY;
Symbols t = s;
like image 82
user207421 Avatar answered Sep 28 '22 06:09

user207421