Enums, mutable fields
Look at second message
class myClass{
public static void main(String[] args) {
// Test the EngineType enum functionality
EngineType engine = EngineType.V8;
// Display engine details
System.out.println("Engine: " + engine.name());
System.out.println("Cylinders: " + engine.getCylinders());
System.out.println("Load Capacity: " + engine.getLoadCapacity());
// Start the engine
engine.startEngine();
// Try starting it again (should show already on)
engine.startEngine();
// Stop the engine
engine.stopEngine();
// Try stopping it again (should show already off)
engine.stopEngine();
}
public enum EngineType { V2(2, 1000), V6(6, 3000), V8(8, 4000), V10(10, 5000), V12(12, 6000); private final int cylinders; private final double loadCapacity; private boolean engineState; // mutable field // Constructor for enum constants EngineType(int cylinders, double loadCapacity) { this.cylinders = cylinders; this.loadCapacity = loadCapacity; this.engineState = false; // default engine state is off } public int getCylinders() { return cylinders; } public double getLoadCapacity() { return loadCapacity; } public boolean isEngineOn() { return engineState; } public void startEngine() { if (!engineState) { engineState = true; System.out.println(this.name() + " engine is starting."); } else { System.out.println(this.name() + " engine is already on."); } } public void stopEngine() { if (engineState) { engineState = false; System.out.println(this.name() + " engine is stopping."); } else { System.out.println(this.name() + " engine is already off."); } } } }
public enum EngineType { V2(2, 1000), V6(6, 3000), V8(8, 4000), V10(10, 5000), V12(12, 6000); private final int cylinders; private final double loadCapacity; private boolean engineState; // mutable field // Constructor for enum constants EngineType(int cylinders, double loadCapacity) { this.cylinders = cylinders; this.loadCapacity = loadCapacity; this.engineState = false; // default engine state is off } public int getCylinders() { return cylinders; } public double getLoadCapacity() { return loadCapacity; } public boolean isEngineOn() { return engineState; } public void startEngine() { if (!engineState) { engineState = true; System.out.println(this.name() + " engine is starting."); } else { System.out.println(this.name() + " engine is already on."); } } public void stopEngine() { if (engineState) { engineState = false; System.out.println(this.name() + " engine is stopping."); } else { System.out.println(this.name() + " engine is already off."); } } } }
7 Replies
⌛
This post has been reserved for your question.
Hey @Ricochet_Master! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
OUTPUT:
Engine: V8
Cylinders: 8
Load Capacity: 4000.0
V8 engine is starting.
V8 engine is already on.
V8 engine is stopping.
V8 engine is already off.
The field engineState is mutable, even though it is in an enum.
Though this seems useful, is this an intended feature?, is it good practice?
it can be useful, but IMO that isn't good practice and will make your code harder to understand for others.
A better approach would be having a wrapper class that has an enum variable as one member and the mutable field(s) as other member
Oh, got it
Thanks a lot
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
One of the quickest and cleanest explanations I have had
Post Closed
This post has been closed by <@763074314206445588>.