Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions advanced_java/enum/Day.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum Day {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use other Enum in built methods as well and show their functionality here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please name a few?

SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
18 changes: 18 additions & 0 deletions advanced_java/enum/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# About Repository
This repository contains the *Enum class* in Java, with examples. Enum is short for "enumerations", which means "specifically listed".<br><br>
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. *Note* that they should be in uppercase letters.

## About files
1. [Day.java][1] : Contains the enum class used in all other files (test class).
1. [inBuiltFunc.java][6] : Showcases built-in Enum methods in Java.
1. [dotSyntax.java][2] : Access enum constants with the dot syntax.
1. [enumInsideClass.java][3] : Having enum inside another class.
1. [enumLoop.java][4] : Returning an array of all enum constants using value() method of enum type.This method is useful when you want to loop through the constants of an enum.
1. [switchCase.java][5] : Using enum in switch case statement.

[1]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/Day.java
[2]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/dotSyntax.java
[3]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/enumInsideClass.java
[4]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/enumLoop.java
[5]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/switchCase.java
[6]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/inBuiltFunc.java
6 changes: 6 additions & 0 deletions advanced_java/enum/dotSyntax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class dotSyntax {
public static void main(String[] args) {
Day variable = Day.TUESDAY
System.out.println(variable);
}
}
6 changes: 6 additions & 0 deletions advanced_java/enum/enumInsideClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class enumInsideClass {
public static void main(String[] args) {
Day myVar = Day.SUNDAY;
System.out.println(myVar);
}
}
7 changes: 7 additions & 0 deletions advanced_java/enum/enumLoop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class enumLoop {
public static void main(String[] args) {
for (Day myVar : Day.values()) {
System.out.println(myVar);
}
}
}
26 changes: 26 additions & 0 deletions advanced_java/enum/inBuiltFunc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enum Day {
MONDAY("Start!"), TUESDAY("Still going."), WEDNESDAY("Still going."), THURSDAY("Almost done."), FRIDAY("Done!"), SATURDAY("FUNday!")
}

//ordinal() method
public class EnumOrdinalExample
{
public static void main(String[] args)
{
// Calling values()
Day arr[] = Day.values();

// enum with loop
for (Day day : arr)
{
// Calling ordinal() to find index of color.
System.out.println(day + " at index "
+ day.ordinal());
}

// Using valueOf(). Returns an object of Day with given constant.
// Uncommenting second line causes exception IllegalArgumentException
System.out.println(Day.valueOf("WEDNESDAY"));
// System.out.println(Color.valueOf("SUNDAY"));
}
}
29 changes: 29 additions & 0 deletions advanced_java/enum/switchCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class switchCase {
public static void main(String[] args) {
Day myVar = Day.WEDNESDAY;

switch(myVar) {
case MONDAY:
System.out.println("Workday.");
break;
case TUESDAY:
System.out.println("Presentation day.");
break;
case WEDNESDAY:
System.out.println("Workday.");
break;
case THURSDAY:
System.out.println("Workday.");
break;
case FRIDAY:
System.out.println("PARTY!");
break;
case SATURDAY:
System.out.println("Relax.");
break;
case SUNDAY:
System.out.println("Complete deadlines.");
break;
}
}
}