Jean Hobbit Jean Hobbit - 2 years ago 81
Java Question

Error with java enum getter converting to int

So I read a few articles here on stackoverflow and feel like I got most of it working, but they don't usually have an example on how to use the getters/setters

I'm super close to figuring out this issue just having a hard time figuring out this last part

Currently all stores in a program i'm using is using openShop(id); but i'd like to be able to use openShop("GENERAL_STORE"); because that is much more maintainable and easy to see without having to look up the id.

Any ideas what I'm doing wrong?

private enum shopName {

private final int id;
shopName(int id) { = id;
public int getId() {

public void openShop(String name) {
int shopId = shopName(name).getId(); //line with error
//int shopId = shopName.shopName(name).getId(); //I've also tried this
public void openShop(int id) {
/* blah */

Here is the error i'm getting

$ ./ error: cannot find symbol
int shopId = shopName(name).getId();
symbol: method shopName(String)
location: class File
1 error

Answer Source

shopName(name) doesn't do what you think it does. This code is trying to invoke method shopName and pass name as argument, but you don't have such method.

If you want to get shopName enum based on provided name use shopName.valueOf(name). So your code should probably look like:

int shopId = shopName.valueOf(name).getId();

Also be aware that this method will throw IllegalArgumentException if you will not provide proper name.

Also enum is considered as type, so its name should start with uppercase like other Java (non-primitive) types. So change shopName to ShopName.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download