public enum Countries {
USA("Washington DC","North America"),
MEXICO("Mexico City","North America"),
CHINA("Beijing","Asia"),
INDIA("New Delhi","Asia"),
FRANCE("Paris","Europe");
}
Q1. Will this code give any compilation error ?
Possible Ans. Yes, we haven't defined constructor with 2 String arguments.
Q2. What is USA, MEXICO , CHINA, INDIA and FRANCE are in this Enum ?
Possible Ans. Enum constants or References to constant objects.
Q3. Can we get to know the continent ( 2nd value in the constructor ) if we have the Enum constant ?
Possible Ans. Yes if we define the constructor and variables appropriately. For example -
public enum Countries {
USA("Washington DC","North America"),
MEXICO("Mexico City","North America"),
CHINA("Beijing","Asia"),
INDIA("New Delhi","Asia"),
FRANCE("Paris","Europe");
Countries(String capital,String continent){
this.capital = capital;
this.continent = continent;
}
public String capital;
public String continent;
}
USA("Washington DC","North America"),
MEXICO("Mexico City","North America"),
CHINA("Beijing","Asia"),
INDIA("New Delhi","Asia"),
FRANCE("Paris","Europe");
}
Q1. Will this code give any compilation error ?
Possible Ans. Yes, we haven't defined constructor with 2 String arguments.
Q2. What is USA, MEXICO , CHINA, INDIA and FRANCE are in this Enum ?
Possible Ans. Enum constants or References to constant objects.
Q3. Can we get to know the continent ( 2nd value in the constructor ) if we have the Enum constant ?
Possible Ans. Yes if we define the constructor and variables appropriately. For example -
public enum Countries {
USA("Washington DC","North America"),
MEXICO("Mexico City","North America"),
CHINA("Beijing","Asia"),
INDIA("New Delhi","Asia"),
FRANCE("Paris","Europe");
Countries(String capital,String continent){
this.capital = capital;
this.continent = continent;
}
public String capital;
public String continent;
}