filmov
tv
enum in Java (Enumeration)- Introduction

Показать описание
You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.
Java Enums Introduction:-
1. According to the java naming conventions, if you have any constant variable, it must be declared in the UPPERCASE.
2. All the constants should be represented with "public static final "as follows
public static final String AVAILABLE="User is available";
public static final String BUSY="User is busy";
public static final String IDLE="User is idle";
3. Java is a strictly typed programming language, the above example will reduce the typedness as it is representing a different data types.
public static final String AVAILABLE="User is available";
public static final int BUSY=0;
public static final boolean IDLE=true;
4. public static final String AVAILABLE="User is not available";
public static final int BUSY=0126262;
public static final boolean IDLE=false;
In the above approach, if we access the constant variables than the values of the constants variables are displayed. However the values may or may not have the meaningful messages with respect to the constant variables.
To overcome all the above problems we are actually going for Java Enums
Enums :-
An enum is a special "class" that represents a group of constants(unchangeable variables, like final variables).
To create an enum, we have to use the "enum" keyword and separate the constants with commas. They should be represented in the upper case letter.
Normal Approach of defining the constants in class
class User_Status {
public static final String AVAILABLE="User is not available";
public static final int BUSY=0126262;
public static final boolean IDLE=false;
}
Defining the constants by using Enums:-
enum User_Status
{
AVAILABLE,
BUSY,
IDLE;
}
Syntax :-
[Access Modifer] enum Name{
//Set of constants
}
Points to be remembered :-
1. All the variables inside the enum type are constant variables and they are public static final by default.
2. All the constant variables are by default of the same enum type not required to provide any explicit data type.
3. All constant variables named constant by default, if we access constant variables name of the constant itself will be displayed, not the value.
Users Code:-
enum User_Status
{
AVAILABLE,
BUSY,
IDLE;
}
Compiler code:-
public static final User_Status AVAILABLE;
public static final User_Status BUSY;
public static final User_Status IDLE;
public static User_Status[] values();
static {};
}
1. Please be informed that you can define the enum, outside the class or inside the class but not inside the methods.
2. You cant create an object for the enum. It means you cant instantiate by using the new keyword
class A{
}
A obj=new A();-- Valid
enum A{
}
A obj=new A();---ERROR
Java Enums Introduction:-
1. According to the java naming conventions, if you have any constant variable, it must be declared in the UPPERCASE.
2. All the constants should be represented with "public static final "as follows
public static final String AVAILABLE="User is available";
public static final String BUSY="User is busy";
public static final String IDLE="User is idle";
3. Java is a strictly typed programming language, the above example will reduce the typedness as it is representing a different data types.
public static final String AVAILABLE="User is available";
public static final int BUSY=0;
public static final boolean IDLE=true;
4. public static final String AVAILABLE="User is not available";
public static final int BUSY=0126262;
public static final boolean IDLE=false;
In the above approach, if we access the constant variables than the values of the constants variables are displayed. However the values may or may not have the meaningful messages with respect to the constant variables.
To overcome all the above problems we are actually going for Java Enums
Enums :-
An enum is a special "class" that represents a group of constants(unchangeable variables, like final variables).
To create an enum, we have to use the "enum" keyword and separate the constants with commas. They should be represented in the upper case letter.
Normal Approach of defining the constants in class
class User_Status {
public static final String AVAILABLE="User is not available";
public static final int BUSY=0126262;
public static final boolean IDLE=false;
}
Defining the constants by using Enums:-
enum User_Status
{
AVAILABLE,
BUSY,
IDLE;
}
Syntax :-
[Access Modifer] enum Name{
//Set of constants
}
Points to be remembered :-
1. All the variables inside the enum type are constant variables and they are public static final by default.
2. All the constant variables are by default of the same enum type not required to provide any explicit data type.
3. All constant variables named constant by default, if we access constant variables name of the constant itself will be displayed, not the value.
Users Code:-
enum User_Status
{
AVAILABLE,
BUSY,
IDLE;
}
Compiler code:-
public static final User_Status AVAILABLE;
public static final User_Status BUSY;
public static final User_Status IDLE;
public static User_Status[] values();
static {};
}
1. Please be informed that you can define the enum, outside the class or inside the class but not inside the methods.
2. You cant create an object for the enum. It means you cant instantiate by using the new keyword
class A{
}
A obj=new A();-- Valid
enum A{
}
A obj=new A();---ERROR