| Author |
Enum: Constructors in constant specific class body
|
Marco Farrugia
Ranch Hand
Joined: Jul 20, 2012
Posts: 33
|
|
|
I am trying to put a constructor in a constant specific class body and apparently this is not legal. Do you not if it NOT possible and why not?
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
Post your code. You need to help us help you.
WP
|
 |
Marco Farrugia
Ranch Hand
Joined: Jul 20, 2012
Posts: 33
|
|
Here it is:
I am trying to add a constructor under CAFE_MOCHA constant specific class body. I am quite new to enums, but I think that with that code I am trying to override the constructor which cannot happen in Java. Correct me if I am wrong.
Thanks,
Marco
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
CAFE_MOCHA by itself will fire the CoffeeType(float) method. Why are you trying to override it?
WP
|
 |
Marco Farrugia
Ranch Hand
Joined: Jul 20, 2012
Posts: 33
|
|
|
I want to define different behaviour on construction of CAFE_MOCHA in particular. Let's say the I want to display a warning that cafe mocha is not lactose free. This is just an example, but the compiler is giving an error indicating that a return type is required at line 4
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
A message? On construction of one of the enum values?
Remember that the construction takes place only once, at the first time the enum is loaded. So are you sure you want the message to appear then? And where would you want it to appear? This doesn't sound like a practical requirement to me.
|
 |
Marco Farrugia
Ranch Hand
Joined: Jul 20, 2012
Posts: 33
|
|
|
I would like to add a constructor to the constant specific class body, the same way a method can be added. Why cannot this be done? I want to understand why. Up to now I know that it cannot be done, since it is not able to compile.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Marco Farrugia wrote:I would like to add a constructor to the constant specific class body, the same way a method can be added. Why cannot this be done? I want to understand why. Up to now I know that it cannot be done, since it is not able to compile.
When you provide a constant-specific class body, what you're actually doing is defining an anonymous inner class that extends the enum. This is why you're able to override methods. You can't add a constructor for this for the same reason that you can't add a constructor to any other anonymous inner class: you don't know what the name is. So at the moment it's interpreting what you've added as a normal method - which needs a return type.
If you really do need to have different behaviour on creation (and I'd question whether that's ever going to be the best solution of a problem, but going along with it...) I think you could do the following:
- Add an empty method in CoffeeType (called init() or similar)
- Add a constructor to CoffeeType that calls this method
- Override init() in the constant-specific body
|
 |
Marco Farrugia
Ranch Hand
Joined: Jul 20, 2012
Posts: 33
|
|
Matthew Brown wrote:
When you provide a constant-specific class body, what you're actually doing is defining an anonymous inner class that extends the enum. This is why you're able to override methods. You can't add a constructor for this for the same reason that you can't add a constructor to any other anonymous inner class: you don't know what the name is. So at the moment it's interpreting what you've added as a normal method - which needs a return type.
If you really do need to have different behaviour on creation (and I'd question whether that's ever going to be the best solution of a problem, but going along with it...) I think you could do the following:
- Add an empty method in CoffeeType (called init() or similar)
- Add a constructor to CoffeeType that calls this method
- Override init() in the constant-specific body
Thanks Matthew. That was really a clear and concise explanation.
|
 |
 |
|
|
subject: Enum: Constructors in constant specific class body
|
|
|