Enum

[Solved] Enum | Scala - Code Explorer | yomemimo.com
Question : enum

Answered by : tushar-kadam

==Enum in Java==
-Enum is a data type that contains a 'fixed set of constants'.
-it can be days of weeks(SUNDAY,MONDAY...SATURDAY), directions(NORTH,SOUTH,
EAST,WEST), colors(RED,YELLOW,BLUE) etc, --> in CAPITAL letters.
=declaration of enum in java-
-can be done inside class or outside class, but not inside method.
-eg. @outside of class
//Color.java
enum Color{ RED, GREEN, //we can declare main method inside enum also & we can run BLUE; //as enum file directly from command prompt.
} // >javac Color.java >java Color
//Main.java
public class Main{ public static void main(String[] args) { Color c1 = Color.RED; System.out.println(c1); }
}
-eg. @inside a class
//Main.java
public class Main{ enum Color{ RED, GREEN, BLUE; } public static void main(String[] args) { Color c1 = Color.RED; System.out.println(c1); }
}
=Note
-Every enum is internally implemented by using class
-Every enum constant represents an object of type enum.
//the above enum color will be internally converted as-
final class Color extends Enum
{ public static final Color RED = new Color(); public static final Color GREEN = new Color(); public static final Color BLUE = new Color();
}
=Enum and inheritance-
-all enums implicitly extends 'java.lang.Enum' class
-as a class can only extend one parent in java, enum cannot extend anything else
-toString() method is overriden in java.lang.Enum class, which returns enum constant name
-enum can implement many interfaces.
=values() and ordinal() methods:
-these methods are present inside java.lang.Enum
-'values()' mehtod return all values present inside enum
-'ordinal()' method, used to find constant's index (order is important in enums)
//inside Test class we have enum Color
//in main method of Test class- Color arr[] = Color.values(); //calling values for(Color col : arr) { System.out.println(col+" at index "+ col.ordinal()); //calling ordinal }
//output- RED at index 0
// GREEN at index 1
-----------------------------------------------------------------------------------
=method inside an enum
-//Item.java
public enum Item{ SUGER, RICE{ //overwritten info method for rice only public void info(){ System.out.println("This is Rice"); } }, SALT; public void info(){ System.out.println("This is grocery item"); }
}
-//Main.java
public class Main{ public static void main(String[] args) { Item[] itr = Item.values(); for(Item item : itr) { item.info(); //op- each time "This is grocery item" is printing //but for RICE, due to overwritten method, "This is Rice" is printing } }
}
=constructor inside enum-
-it is executed separately for each enum constant at the time of enum class loading
-we can't create enum objects explicitly and hence we can't invoke enum constructor directly.
-constructor of enum type is private(if you don't declare private, compiler internally creates private constructor)
enum Season{ WINTER(10),SUMMER(20); private int value; Season(int value) //constructor, compiler will add 'private' type prior Season. { this.value=value; }
}
-------------------------------------------------------------------------------
-refer city and Enum in switch case from assignment pages.
-------------------------------------------------------------------------------

Source : | Last Update : Tue, 29 Nov 22

Question : enum python

Answered by : outstanding-okapi-u63tuyb6dzox

>>> from enum import Enum
>>> class Color(Enum):
... RED = 1
... GREEN = 2
... BLUE = 3
...

Source : https://docs.python.org/fr/3/library/enum.html | Last Update : Fri, 30 Apr 21

Question : c# enum syntax

Answered by : jay-jack

enum State
{	Idle, Walking, Running, Jumping, Climbing, Attacking
}

Source : | Last Update : Tue, 22 Sep 20

Question : enum

Answered by : shahul

enum UserResponse { No = 0, Yes = 1,
}
function respond(recipient: string, message: UserResponse): void { console.log(recipient, message); // "Princess Caroline", 1
}
respond("Princess Caroline", UserResponse.Yes);

Source : https://www.typescriptlang.org/docs/handbook/enums.html | Last Update : Sat, 15 Jan 22

Answers related to enum

Code Explorer Popular Question For Scala