[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1569998245539{background-color: #f6f6f6 !important;}”]
Enum in Java is a data type that allows a variable to assume values of predefined constants.
[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
In computer programming, a so-called Enumeration (also called the enumerated type, enum, or categorical variable in statistics) is something that is used to represent a group of named constants. There are also built-in Enumerations in the programming language. For example, Boolean is a predefined enumeration that can only assume the values True or False.
In other words, if you want a variable that should only be able to assume certain predetermined values, the data type is often preferred. For example, if you want a variable that can only accept seasons (spring, summer, autumn, and winter). We can pre-define these four values to ensure that the data type does not adopt values other than those desired.[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
Let’s take a short example where we create a simple program for the four different seasons. Note that we use the reserved word enum instead of class.
Start by creating our class
public class Enum {
Predefine the values for the Season variable
public enum Season{
SPRING, SUMMER, FALL, WINTER
}
Then we create a data type of Enum type. Sets the value to SPRING
public static Season season = Season.SPRING;
Then to try our code we can simply assign the Enum some of our predefined options and print them:
public static void main (String args[]){
System.out.println(season);
// Sets the value of season to SUMMER
season = Season.SUMMER;
System.out.println(season);
// Sets the value of season to FALL
season = Season.FALL;
System.out.println(season);
// Sets the value of season to WINTER
season = Season.WINTER;
System.out.println(season);
}
Note: If we write, for example,
season = Season.AUTUMN;
our program crashes and gets an error-message since AUTUMN is not predefined as the allowed variable in Season
To conclude, although this may seem like a simple example, it is only to show how Enum in Java works and to build an understanding of how we use it. The complete code to the example used above
public class Enum {
public enum Season{
SPRING, SUMMER, FALL, WINTER
}
public static Season season = Season.SPRING;
public static void main (String args[]){
System.out.println(season);
// Sets the value of season to SUMMER
season = Season.SUMMER;
System.out.println(season);
// Sets the value of season to FALL
season = Season.FALL;
System.out.println(season);
// Sets the value of season to WINTER
season = Season.WINTER;
System.out.println(season);
}
}
That was a short example, for more info on enum as well as a summary of useful methods see the Oracle website[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
In our game we will have an enum with two states:
Thanks to enum, we can easily code what to draw on the game screen (Either the game with the falling wall, the player and the calculator – or the text that prompts the user to start the game, in other words, press enter)[/vc_column_text][vc_empty_space height=”50px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_single_image image=”17407″ img_size=”medium” style=”vc_box_border” qode_css_animation=””][vc_column_text]
RESTART
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_single_image image=”17406″ img_size=”medium” style=”vc_box_border” qode_css_animation=””][vc_column_text]
GAME
[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_empty_space height=”30px”][vc_column_text]
[/vc_column_text][vc_empty_space][/vc_column][/vc_row]