Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I able to call a Enums constructor in Java?

I want to make a new record in my enum list of usernames and passwords. I was going to make a method called register that would create a new record by calling the enum's constructor but when I call the constructor like this:

public void register(String usr, String pass) {
        DB(usr, pass); //DB is the name of my enum
}

I get a error saying to create a new method. How am I able to call a constructor of my enum

Thanks for any help!

like image 627
Fouroh3 Avatar asked Oct 16 '25 14:10

Fouroh3


1 Answers

Enums are types with a closed set of instances. These instances are defined at class-design time (they are defined in the source code of the enum).

You cannot add enum instances at runtime. Enum constructors are private by language restrictions.

like image 185
Andrei Nicusan Avatar answered Oct 18 '25 13:10

Andrei Nicusan