diff --git a/advanced_java/enum/Day.java b/advanced_java/enum/Day.java new file mode 100644 index 0000000..afb852d --- /dev/null +++ b/advanced_java/enum/Day.java @@ -0,0 +1,4 @@ +public enum Day { + SUNDAY, MONDAY, TUESDAY, WEDNESDAY, + THURSDAY, FRIDAY, SATURDAY +} diff --git a/advanced_java/enum/Readme.md b/advanced_java/enum/Readme.md new file mode 100644 index 0000000..7058cf4 --- /dev/null +++ b/advanced_java/enum/Readme.md @@ -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".

+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 diff --git a/advanced_java/enum/dotSyntax.java b/advanced_java/enum/dotSyntax.java new file mode 100644 index 0000000..aff1d0b --- /dev/null +++ b/advanced_java/enum/dotSyntax.java @@ -0,0 +1,6 @@ +public class dotSyntax { + public static void main(String[] args) { + Day variable = Day.TUESDAY + System.out.println(variable); + } +} diff --git a/advanced_java/enum/enumInsideClass.java b/advanced_java/enum/enumInsideClass.java new file mode 100644 index 0000000..e896cbe --- /dev/null +++ b/advanced_java/enum/enumInsideClass.java @@ -0,0 +1,6 @@ +public class enumInsideClass { + public static void main(String[] args) { + Day myVar = Day.SUNDAY; + System.out.println(myVar); + } +} diff --git a/advanced_java/enum/enumLoop.java b/advanced_java/enum/enumLoop.java new file mode 100644 index 0000000..53c0d7c --- /dev/null +++ b/advanced_java/enum/enumLoop.java @@ -0,0 +1,7 @@ +public class enumLoop { + public static void main(String[] args) { + for (Day myVar : Day.values()) { + System.out.println(myVar); + } + } +} diff --git a/advanced_java/enum/inBuiltFunc.java b/advanced_java/enum/inBuiltFunc.java new file mode 100644 index 0000000..06bd18f --- /dev/null +++ b/advanced_java/enum/inBuiltFunc.java @@ -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")); + } +} diff --git a/advanced_java/enum/switchCase.java b/advanced_java/enum/switchCase.java new file mode 100644 index 0000000..be18e47 --- /dev/null +++ b/advanced_java/enum/switchCase.java @@ -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; + } + } +}