user6952090
user6952090

Reputation: 63

Insertion into EnumMaps Errors

I have a Vehicle class containing an enum Vehicle Type as shown below

public abstract class Vehicle{

   public enum VehicleType
   {
    ECONOMY, COMPACT, SUV;
   }
 //method variables..getters and setters below
}

I am working in a different class called CarReservation now and I am having trouble inserting values into the enumMap to track inventory as follows

import packageName.Vehicle.*;
import packageName.VehicleType;
public class CarReservation {

/*public enum VehicleType
{
    //ECONOMY, COMPACT, SUV;
}*/  
//do I need to include the enum in this class as well?

public static final int MAX_ECONOMY = 10;
public static final int MAX_SEDAN = 5;  
public static final int MAX_SUV = 5;

Map<VehicleType, Integer> availEconomy =  new EnumMap<VehicleType, Integer>(VehicleType.class);
 availEconomy.put(VehicleType.ECONOMY, MAX_ECONOMY); //Eclipse gives me an error saying constructor header name expected here.
}

I am trying to create a way of tracking counts for the different vehicle Types. Can someone tell me what's wrong with my approach? Thanks!

Upvotes: 0

Views: 114

Answers (1)

Farman
Farman

Reputation: 66

In your case availEconomy is instance variable and in java you can put value in instance variable in method only. or else you can define availEconomy static and put values inside static block. Below is the code for that.

public class CarReservation {

/*
 * public enum VehicleType { //ECONOMY, COMPACT, SUV; }
 */
// do I need to include the enum in this class as well?

public static final int MAX_ECONOMY = 10;
public static final int MAX_SEDAN = 5;
public static final int MAX_SUV = 5;

static Map<VehicleType, Integer> availEconomy = new EnumMap<VehicleType, Integer>(VehicleType.class);
static {
    availEconomy.put(VehicleType.ECONOMY, MAX_ECONOMY);
}

}

Upvotes: 1

Related Questions