Reputation: 10502
I have this enum singleton class:
enum C1 {
INSTANCE("");
C1(String s) {
System.out.println("with param = " +s);
}
C1() {
System.out.println("without param");
}
public void g() {
System.out.println("inside g");
}
}
public class Main {
public static void main(String s[]) {
C1.INSTANCE.g();
C1.INSTANCE.g();
}
}
How can i call C1(String s)
constructor using INSTANCE
by passing custom parameter ?
Upvotes: 0
Views: 547
Reputation: 824
Generally you can use class type, not enum type. Enums are intended to be static and final. The idea of the enum constructor is to encapsulate several values so you can use them after by setting only one type of enum.
public enum Vehicle {
CAR(4, 2),
TRUCK(6, 20);
private Integer numOfTyres;
private Integer maxWeight;
Vehicle(Integer numOfTyres, Integer maxWeight) {
this.numOfTyres = numOfTyres;
this.maxWeight = maxWeight;
System.out.println("Number of Tyres = " + numOfTyres);
System.out.println("Max weight = " + maxWeight);
}
public Integer getMaxWeight() {
return maxWeight;
}
public Integer getNumberOfTyres() {
return numOfTyres;
}
}
public class Main {
public static void main(String s[]) {
Vehicle.CAR.getNumberOfTyres();
for (Vehicle v : Vehicle.values()) {
System.out.println();
StringBuilder sb = new StringBuilder();
sb.append("Vehicle ");
sb.append(v.name());
sb.append(" has ");
sb.append(v.getNumberOfTyres());
sb.append(" number of tyres and ");
sb.append(v.getMaxWeight());
sb.append(" max weight ");
System.out.println("-----------------");
System.out.println(sb.toString());
System.out.println("-----------------");
}
}
}
Upvotes: 1
Reputation: 5354
As you probably know - the constructor for an enum type must be package-private or private access.
It is called automatically and creates the constants that are defined at the beginning of the enum body.
You cannot invoke an enum constructor yourself.
Here are some examples that might be helpful.
public enum MySingleton {
// if you have multiple constants here - it's not Singleton
INSTANCE;
// if you need to provide some info to your INSTANCE,
// you can add params to its methods
public void someMethod(String value) {
System.out.println("inside someMethod, value is " +value);
}
}
If you need the instance to have some state you can add fields and a constructor:
public enum MySingleton {
INSTANCE("Hello");
private String message;
MySingleton(String msg) {
this.message = msg;
}
// you can even have getter/setter for it
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Main method sample:
public static void main(String[] args) {
System.out.println(MySingleton.INSTANCE.getMessage()); // prints Hello
MySingleton.INSTANCE.setMessage("Bye");
System.out.println(MySingleton .INSTANCE.getMessage()); // prints Bye
}
Upvotes: 0
Reputation: 2220
You can have someting like that
enum C1 {
WITH_PARAM("value"),
EMPTY();
private String value;
C1(String s) {
System.out.println("with param = " +s);
value=s;
}
C1() {
System.out.println("without param");
}
public void g() {
System.out.println("inside g, value is "+value);
}
}
public static void main(String s[]) {
C1.EMPTY.g();
C1.WITH_PARAM.g();
}
Upvotes: 2