How to pass enum as an argument in a method in java?

后端 未结 6 1107
走了就别回头了
走了就别回头了 2021-01-03 18:47
public class Enumvalues{

    enum courseList {
        JAVA,
        C,
        PYTHON,
        PERL
    }

    enum generalInformation {
        NAME,
        AGE,         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 19:14

    An enum is a class. So you can pass an instance of the class (EnumValues.generalInformation.PHONE for example), or you can pass the class itself (EnumValues.generalInformation.class for example).

    If you want to list all the instances of an enum class, you need to pass the enum class, and use EnumSet.allOf(EnumValues.generalInformation.class) for example.

    Your confusion principally comes from the fact that you don't respect the Java naming conventions. ENums are classes and should start with an upper-case letter (GeneralInformation for example). An other source of confusion is the bad choice of names. JAVA is not a course list. It's a course. So the enum should be named Course.

提交回复
热议问题