Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make objects of an enum in java

Tags:

java

I am trying to define three different objects of enum class "Item" with weights between 0 and 20 and description of each item

Here is my code :

public enum Item
{
    // Three Items with descriptions
    GOLD (2, "gold"), SILVER(12, "silver"), BRONZE(5, "bronze");

    private int weight;
    private String description;

    public int getWeight()
    {
       return weight;
    }

    public String toString()
    {
        return description;
    }

}

I keep getting this error

constructor Item in enum Item cannot be applied to given types: required: no arguments, found:int.java.lang.String, reason: actual and formal argument lists differ in length

Also

The operator that you use here cannot be used for the type of value that you are using it for. You either using the wrong type here, or the wrong operator

like image 732
tb1000 Avatar asked Feb 10 '15 16:02

tb1000


People also ask

Can we create object of enum in Java?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Can enum values be objects?

An enum is a data type that can be created by a Java programmer to represent a small collection of possible values. Technically, an enum is a class and its possible values are objects.

How do you create an instance of an enum in Java?

10) You can not create an instance of enums by using a new operator in Java because the constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself. 11) An instance of Enum in Java is created when any Enum constants are first called or referenced in code.

Can you make a interface for an enum?

Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. An Enum is a special datatype which is added in Java 1.5 version.


1 Answers

You forgot to declare a constructor !

public enum Item {

  GOLD(2, "gold"), SILVER(12, "silver"), BRONZE(5, "bronze");     

  private int weight;
  private String description;

  private Item(int weight, String description) {
    this.weight = weight ;
    this.description = description ;
  }

  // rest of code

}

Long story short
In your code GOLD(2, "gold") can be read as GOLD = new Item(2, "gold"), so the compiler doesn't find the appropriate constructor.

More complete answer and examples
Don't forget that everything (well, almost everything, there are some primitive like int or boolean) is an object in Java. And so, items of your enum are also objects, and need to be instantiate with a constructor.
Keep in mind that there is an implicit parameterless constructor if you don't provide an explicit constructor.

public enum Test {
  A, B, C;
}

public enum Test2 {
  A, B, C;

  private int value ;

  private Test2(){
    value = 0;
  }
}

public enum Test3 {
  A(1), B(2), C(3);
}

public enum Test4 {
  A, B, C;

  public Test4(int value) {}
}

Test and Test2 are valid, because there is a parameterless constructor (implicit for Test). But Test3 and Test4 try to instantiate A, B and C with a constructor which doesn't exist.

like image 75
NiziL Avatar answered Oct 04 '22 08:10

NiziL