Enum private Constructor
enum has constructor. This constructor is private. Why do we need a private constructor? because enum has finite values. If the constructor is public , anyone can change the values of enum. It can increase the values This would extend the initial values of enum.
It is a compile time error if the constructor of an enum type is declared as public or protected. If no access modifier is specified for the constructor of an enum type, The constructor is by default private.
package com.simple
String desc) {
this.desc = desc;
}
public String getDesc(){
return desc;
}
Here we have a BankAccount constructor without any access modifier. In case of enum is by default private.
But in case of class declaration constructor without access modifier will be considered as default
package com.simple
for (BankAccount acc : BankAccount.values()) {
System.out.println ("Account " + acc + "values" + acc.getDec())
}
Comments
Post a Comment