2 Computer Programming Module 7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

MODULE 7 - ENUMS

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)

public enum Directions{


EAST,
WEST,
NORTH,
SOUTH
}
public class EnumDemo
{
public static void main(String args[]){
Directions dir = Directions.NORTH;
if(dir == Directions.EAST) {
System.out.println("Direction: East");
} else if(dir == Directions.WEST) {
System.out.println("Direction: West");
} else if(dir == Directions.NORTH) {
System.out.println("Direction: North");
} else {
System.out.println("Direction: South");
}
}
}
MODULE 7 - ENUMS

Lesson 1 – Enums

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:

Enum inside a class

You can also have an enum inside a class:

Enum in a Switch Statement


MODULE 7 - ENUMS

Enums are often used in switch statements to check for corresponding values:

Loop Through an Enum

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:

Difference between Enums and Classes


MODULE 7 - ENUMS

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).

Why And When To Use Enums?

Use enums when you have values that you know aren't going to change, like month days, days, colors,
deck of cards, etc.

Lesson 2 - public static void main

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.

5. String[] args: It stores Java command line arguments and is an array of type java.lang.String class.


Here, the name of the String array is args but it is not fixed and user can use any name in place of it.

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

public enum Directions{


EAST,
WEST,
NORTH,
SOUTH
}
public class EnumDemo
{
public static void main(String args[]){
Directions dir = Directions.NORTH;
if(dir == Directions.EAST) {
System.out.println("Direction: East");
} else if(dir == Directions.WEST) {
System.out.println("Direction: West");
} else if(dir == Directions.NORTH) {
System.out.println("Direction: North");
} else {
System.out.println("Direction: South");
}
}
}

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/

You might also like