Enquiries for Enum in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Enquiries for Enum in java

How can I declare enum inside the class? Like how can I modify to make sure my code runs? Take note(Enum must be inside the class) class Sportperson { enum Country{ Usa, Brazil, Indonesia}; private Country home; private String player_name; public Sportperson (Country home, String player_name ) { this.home=home; this.player_name = player_name ; } public void printDetails() { System.out.printf("The player's country is %s",home); System.out.printf("name: %s",player_name ); } } public class Program { public static void main(String[] args) { Sportperson mvp = new Sportperson (Country.Usa, "Mike"); mvp.printDetails (); } }

11th Apr 2022, 10:08 AM
2 Answers
+ 1
Since this enum will need to be accessible from outside the class, set it public. public enum Country{ Usa, Brazil, Indonesia }; Specify class name where the enum was defined when referencing it. Sportperson mvp = new Sportperson ( Sportperson.Country.Usa, "Mike" );
11th Apr 2022, 10:44 AM
11th Apr 2022, 12:18 PM