How to use enum copy constructor in Java?

I am trying to complete a project, and although I tried, I cannot do it. Here's the listing:

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 character named "s" as follows:

 Symbols s = Symbols.CHERRY; 

But I had problems creating a copy of "s" using the created copy constructor:

 Symbols t = Symbols(s); 

If I try to do this, I get an error: "The characters (characters) of the method are undefined for characters of type."

thanks

+4
source share
6 answers

Well, you need to make it publicly available, but it still won't work. There are no copy constructors in Java; you cannot "new" Enum, which is the next error; and in any case, Java goes to great lengths to ensure that there is only one copy of each enum value in the JVM. Why do you think you need a copy constructor?

Decision:

 Symbols s = Symbols.CHERRY; Symbols t = s; 
+4
source

Enumerations must be an immutable set - you must not create new ones. And the constructors are forcibly applied as private (although not marked as such).

Since they are immutable, you should always be sure that if you have an instance of a Symbols.CHERRY object, it is always the same instance, so you can do things like checking the equivalence of objects, not equal ones - i.e. you can do

 if (symbol == CHERRY) 

instead

 if (symbol.equals(CHERRY)) 

because if you have CHERRY, they are guaranteed to be the same object.

This is more like a forced version of the Typesafe Enum template:

 public class Suit { private final String name; public static final Suit CLUBS =new Suit("clubs"); public static final Suit DIAMONDS =new Suit("diamonds"); public static final Suit HEARTS =new Suit("hearts"); public static final Suit SPADES =new Suit("spades"); private Suit(String name){ this.name =name; } } 
+2
source

Well, constructors are only called when the new keyword is called:

 Symbols t = new Symbols(s); 

Without new he thinks you're trying to call a method called Symbols .

But that does not matter in this case, because you cannot create an instance of enum outside of constant definitions. If you could, this was not an enumeration.

Let's look at why you ever wanted to have a copy of the listing. If you need a copy, this is necessary because you are saying that an enumeration is a condition that should not be true. Enumerations must be unchanged. And since they must be immutable, you can share instances anywhere.

+2
source

You are using ENUM something like this:

 public enum Symbols { SEVEN(12,"images/seven.jpg"), WATERMELON(10,"images/watermelon.jpg"); private int payout; private String icon; Symbols(int payout, String icon){ this.payout = payout; this.icon = icon; } public int getPayout(){ return this.payout; } public String getIcon(){ return this.icon; } public static void main(String[] args){ System.out.println(Symbols.SEVEN.getPayout()); } } 

Other answers provided a good explanation of why you don't have copy constructors. Basically, the "instances" (SEVEN and WATERMELON) of your enum class are immutable, so you cannot specify any other value inside them at runtime, and you cannot create new ones.

+1
source

enums are not normal classes. you cannot manually create them. the "enum" class itself is basically an "abstract" class. each value is implemented as a separate concrete subclass of the enumeration class "class".

0
source

Enum are classes that do not allow the creation of new objects. So your character class (enum) has only six instances. You cannot call the enumeration constructor, but you can have a copy of the links created for six instances.

 Symbols s = Symbols.CHERRY; //Get a copy of reference for instance CHERRY 

On the other hand, if you need a high constructor in Java, you need to use the keyword "new".

0
source

All Articles