TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Saturday, 8 February 2020

enumeration in java | java enum example with values java enum key value

Enum in Java

Enumerations serve the purpose of representing a group of named constants in a programming language. For example the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. Other examples include natural enumerated types (like the planets, days of the week, colors, directions, etc.).
Enums are used when we know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time.
In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums . In Java, we can also add variables, methods and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).

java enum example with values | java enum key value
java enumeration 

 Declaration of enum in java :

·         Enum declaration can be done outside a Class or inside a Class but not inside a Method.

  // A simple enum example where enum is declared
  // outside any class (Note enum keyword instead of
 // class keyword)
  enum Color
  {
          RED, GREEN, BLUE;
}

   public class Test
   {
          // Driver method
          public static void main(String[] args)
          {
                   Color c1 = Color.RED;
                   System.out.println(c1);
          }
}

  Output:

  RED
·         First line inside enum should be list of constants and then other things like methods, variables and constructor.
·         According to Java naming conventions, it is recommended that we name constant with all capital letters

  Important points of enum :

·         Every enum internally implemented by using Class.

/*internally above enum color is converted to
class Color
{
public static final Color RED=new Color();
public static final Color BLUE=new Color();
public static final Color GREEN=new Color();
}*/

·         Every enum constant represents an object of type enum.
·         enum type can be passed as an argument to switch statement.
·            Every enum constant is always implicitly public static final. Since it is static, we can access it by using enum Name. Since it is final, we can’t create child enums.
·         We can declare main() method inside enum. Hence we can invoke enum directly from the Command Prompt.

No comments:

Post a Comment