Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Enums with Methods | Advanced Enum Features
Working with Java Enum

bookEnums with Methods

In Java, enums are more than just collections of constantsβ€”they can also contain methods, fields, and even abstract methods that each constant must implement. By adding methods to an enum, you can give each constant its own behavior, making your code more flexible and easier to maintain. One powerful technique is using constant-specific class bodies, which allow you to override methods for individual constants, much like subclassing. This is especially useful when you want each enum constant to behave differently for a shared operation.

Operation.java

Operation.java

copy
12345678910111213141516171819202122232425262728293031
package com.example; public enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; public abstract double apply(double x, double y); }

By using constant-specific class bodies, you can give each enum constant its own implementation of a method. This approach is closely related to the strategy pattern in object-oriented design, where you define a family of algorithms, encapsulate each one, and make them interchangeable. With enums, you can bundle related behaviors directly with the constants, reducing the need for external classes or switch statements and making your code more concise and expressive.

EnumMethodDemo.java

EnumMethodDemo.java

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package com.example; public class Main { public static void main(String[] args) { double x = 10.0; double y = 5.0; for (Operation op : Operation.values()) { System.out.println(op + ": " + op.apply(x, y)); } } } enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; abstract double apply(double x, double y); }

1. What is a constant-specific class body in an enum?

2. Can enums have abstract methods?

3. When would you use methods inside an enum?

question mark

What is a constant-specific class body in an enum?

Select the correct answer

question mark

Can enums have abstract methods?

Select the correct answer

question mark

When would you use methods inside an enum?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show an example of how to implement an enum with methods in Java?

How does using methods in enums compare to using switch statements?

What are some real-world scenarios where this enum strategy pattern is useful?

bookEnums with Methods

Swipe to show menu

In Java, enums are more than just collections of constantsβ€”they can also contain methods, fields, and even abstract methods that each constant must implement. By adding methods to an enum, you can give each constant its own behavior, making your code more flexible and easier to maintain. One powerful technique is using constant-specific class bodies, which allow you to override methods for individual constants, much like subclassing. This is especially useful when you want each enum constant to behave differently for a shared operation.

Operation.java

Operation.java

copy
12345678910111213141516171819202122232425262728293031
package com.example; public enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; public abstract double apply(double x, double y); }

By using constant-specific class bodies, you can give each enum constant its own implementation of a method. This approach is closely related to the strategy pattern in object-oriented design, where you define a family of algorithms, encapsulate each one, and make them interchangeable. With enums, you can bundle related behaviors directly with the constants, reducing the need for external classes or switch statements and making your code more concise and expressive.

EnumMethodDemo.java

EnumMethodDemo.java

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package com.example; public class Main { public static void main(String[] args) { double x = 10.0; double y = 5.0; for (Operation op : Operation.values()) { System.out.println(op + ": " + op.apply(x, y)); } } } enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; abstract double apply(double x, double y); }

1. What is a constant-specific class body in an enum?

2. Can enums have abstract methods?

3. When would you use methods inside an enum?

question mark

What is a constant-specific class body in an enum?

Select the correct answer

question mark

Can enums have abstract methods?

Select the correct answer

question mark

When would you use methods inside an enum?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt