2 Computer Programming Module 7
2 Computer Programming Module 7
Pre-test: Guess what will be the output: As the instruction stated, guess the output and write it in a one-fourth
sheet of paper: (10 points)
Lesson 1 – Enums
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:
The enum type has a values() method, which returns an array of all enum constants. This method is
useful when you want to loop through the constants of an enum:
An enum can, just like a class, have attributes and methods. The only difference is that enum constants
are public, static and final (unchangeable - cannot be overridden).
An enum cannot be used to create objects, and it cannot extend other classes (but it can implement
interfaces).
Use enums when you have values that you know aren't going to change, like month days, days, colors,
deck of cards, etc.
In Java programs, the point from where the program starts its execution or simply the entry point of Java
programs is the main() method. Hence, it is one of the most important methods of Java and having proper
understanding of it is very important.
Explanation:
Every word in the public static void main statement has got a meaning to the JVM.
1. Public: It is an Access modifier, which specifies from where and who can access the method. Making
the main() method public makes it globally available. It is made public so that JVM can invoke it from
outside the class as it is not present in the current class.
2. Static: It is a keyword which is when associated with a method, makes it a class related method.
The main() method is static so that JVM can invoke it without instantiating the class. This also saves the
MODULE 7 - ENUMS
unnecessary wastage of memory which would have been used by the object declared only for calling
the main() method by the JVM.
3. Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method
doesn’t return anything, its return type is void. As soon as the main() method terminates, the java
program terminates too. Hence, it doesn’t make any sense to return from main() method as JVM can’t
do anything with the return value of it.
4. main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point
of the java program. It’s not a keyword.
Apart from the above mentioned signature of main, you could use public static void main(String
args[]) or public static void main(String… args) to call the main function in java. The main method is called
if it’s formal parameter matches that of an array of Strings.
Pre-test: Guess what will be the output: As the instruction stated, guess the output and write it in a one-fourth
sheet of paper, and code it using DCoder app and see the difference: (10 points)
MODULE 7 - ENUMS
Some Notes:
1. Post-test will be recorded and the instructor will collect your answers.
2. Activities will also be recorded and the instructor will collect your exercises.
Bibliography/References:
https://www.w3schools.com/java/java_enums.asp
https://www.geeksforgeeks.org/understanding-public-static-void-mainstring-args-in-java/