-
Book Overview & Buying
-
Table Of Contents
Java Fundamentals
By :
Enumeration in Java (or enum) is a special type in Java whose fields consist of constants. It is used to impose compile-time safety.
For example, consider the days of the week, they are a set of fixed constants, therefore we can have an enum defined:
public enum DayofWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
} Now we can simply check if a variable that stores a day is part of the declared enum. We can also declare enums for non-universal constants, such as:
public enum Jobs {
DEVELOPER, TESTER, TEAM LEAD, PROJECT MANAGER
}This will enforce the job-type to be the constants declared in the Jobs enum. Here's an example enum holding currencies:
public enum Currency {
USD, INR, DIRHAM, DINAR, RIYAL, ASD
}We will create an enum and will find values and compare enums.
Create a class EnumExample and in the main method. Get and print the enum using the value as enum. Get and print enum using the values...
Change the font size
Change margin width
Change background colour