- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a Type-safe Enum in Java?n
The enums are type-safe means that an enum has its own namespace, we can’t assign any other value other than specified in enum constants. Typesafe enums are introduced in Java 1.5 Version. Additionally, an enum is a reference type, which means that it behaves more like a class or an interface. As a programmer, we can create methods and variables inside the enum declaration.
Example1
import java.util.*; enum JobType { permanent, contract } public class EnumTest1 { public static void main(String []args) { print(JobType.values()); } public static void print(JobType[] list) { for (int i=0; i < list.length; i++) { System.out.println(list[i]); } } }
Output
permanent contract
Example2
enum JobType { permanent { public void print(String str1) { System.out.println("This is a permanent " + str1); } }, contract { public void print(String str2) { System.out.println("This is a contarct " + str2); } }; abstract void print(String name); } public class EnumTest2 { public static void main(String[] args) { JobType dt1 = JobType.permanent; JobType dt2 = JobType.contract; dt1.print("job"); dt2.print("job"); } }
Output
This is a permanent job This is a contract job
- Related Articles
- What is a Type-safe Enum in Java?
- What is Type safe in C#?
- What is MySQL ENUM data type? What are the advantages to use ENUM data type?
- How do we use an enum type with a constructor in Java?
- The equals and == operator for Enum data type in Java
- Is Java matcher thread safe in Java?
- Is Swing thread-safe in Java?
- Enum in Java
- Enum in a class in Java
- What is the difference between Enumeration interface and enum in Java?
- what are the different attributes of MySQL ENUM data type?
- Count items in a MySQL table with type ENUM involved?
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?
- What is type conversion in java?
- Print all safe primes below N in C++
- Enum Methods in Java

Advertisements