enum in Java(Enumeration) - Part 2 - The best way to represent constants in Java

preview_player
Показать описание
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.

In Java Enums,
class A extends Object
{
//variables(instantance or static variables)
//constructors
//methods
}
class B extends A
{
}

ENUM ----- CONSTANTS
1. Unlike java classes, enums neither inherit other classes nor can get extended.
2. In java enums, we can add variables, methods and constructors to it.
3. The Main objective is to define our own data types.
How to declare an Enum in Java?
1. Declaration can be done outside the class.
enum Directions{
NORTH,SOUTH,EAST,WEST;
}
public class Test{
public static void main(String[] args)
{
Directions d1=Directions.SOUTH;
}
}
2 Declaration can be done inside the class.
public class Test{
enum Directions{
NORTH,SOUTH,EAST,WEST;
}
public static void main(String[] args)
{
Directions d1=Directions.SOUTH;

}
}

Points to be noted:-
1. The first line inside the enum should be a list of constants and than you can add other things like methods, variables and constructors.
//Valid
enum A
{
RED,BLUE,GREEN;
//Variable
//Constructors
//methods

}
enum A
{
//Invalid
//Variable
//Constructors
//methods
RED,BLUE,GREEN;
}

2. Please be informed that according to the Java naming conventions, it is highly recommended that we name constansts with UPPERCASE.

Properties of ENUM in java:-

1.Please be informed that every enum is internally implemented by using a class.
2.Every enum constant represents an Object of type enum.
3.You can pass the enum constants to a java Switch statements.
4.Every enum constant is always implicitly public static final .Since, it is static , we can access it by using the enum Name. Since, it is final we cant create the child enums.
5.We can declare the main() method inside the enum. Hence, we can invoke the enum directly from the command prompt.

Q) Can we declare a main method inside java enums?
Yes, we can.
Example:-
enum Directions{
NORTH,SOUTH,EAST,WEST;
public static void main(String[] args)
{
Directions d1=Directions.SOUTH;
}
}
class A
{
psvm()
{
//main method inside a class
}
}

enum A
{
psvm()
{
//Defining main method inside an enum
}

}
Can we do the looping through ENUM constants?
Yes, we can. We will have to Iterate through the constants by using values() method.
Example:-
enum Apple
{

A(500),B(400),C(300);
int price;
Apple(int price)
{
}
public int getPrice()
{
return price;
}

}

Compiler code:-
{
public static final Apple B=new B(400);
public static final Apple C=new C(300);
int price;
Apple(int price)
{
}
public int getPrice()
{
return price;
}
}

What is the purpose of the values() method in the enum?
The values() method is used to return all the values present in the enum with the help of enhanced for loop.
Can we create an object or instace for an enum?
No, we cant create an object or instance for enum.
Рекомендации по теме
welcome to shbcf.ru