Java Enums
Represent a fixed set of named choices.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Enums.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
An enum models a closed set of constants, such as the states in a simple workflow. Compared with arbitrary strings, it gives the compiler a known set of values. Enum names are identifiers; display text can be handled separately.
விரிவான விளக்கங்கள்
Java Enums என்னன்னு தெரியுமா?
Enum என்பது constants-ஓட group-ஐ represent பண்றது — உதா: days of the week, directions, seasons.
Enum-ஐ enum keyword use பண்ணி create பண்றோம்:
Enum in Switch
Enum-ஐ switch statement-ல use பண்ணலாம்:
Enum constants-ஐ uppercase-ல எழுதுங்கள்வது convention — இதுவே best practice.
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
Day today = Day.WED;
System.out.println(today); // WED
System.out.println(today.name()); // WED
System.out.println(today.ordinal()); // 2
switch (today) {
case SAT: case SUN:
System.out.println("Weekend!"); break;
default:
System.out.println("Weekday");
}
for (Day d : Day.values()) {
System.out.print(d + " ");
}
// MON TUE WED THU FRI SAT SUNJava Enum-ன் முக்கிய advantage என்ன?
- Memory குறைவாக use செய்யும்
- Type-safe constants — invalid values compile time-லேயே catch ஆகும்
- Faster execution
- Multiple inheritance support
Reveal answer / விடை
Type-safe constants — invalid values compile time-லேயே catch ஆகும்
Enum type-safe constants தரும். `DayOfWeek day = DayOfWeek.MONDAY;` — `"MONDAYY"` போன்ற typos compile time-லேயே error காட்டும். String constants use செய்வதை விட மிகவும் safe.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Status status = Status.IN_PROGRESS;
System.out.println(status);
System.out.println(status == Status.IN_PROGRESS);
}
}
enum Status { NEW, IN_PROGRESS, DONE }Predict first. Then reveal the expected output / வெளியீடு
IN_PROGRESS true
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Change status to DONE and predict the equality result.
Reveal the answer and reasoning / விடை
The first line becomes DONE, and the comparison becomes false.
தவிர்க்க வேண்டிய தவறு: Avoid coupling stored business data to ordinal positions. Inserting an enum constant can change those positions.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.