首页 > 编程语言 >Java: Enums

Java: Enums

时间:2022-11-27 07:11:06浏览次数:41  
标签:MEDIUM Java Level enum myVar Enums constants

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

You can access enum constants with the dot syntax:

Level myVar = Level.MEDIUM;

Loop Through an Enum

The enum type has a values() method, which returns an array of all enum constants.

for (Level myVar : Level.values()) {
  System.out.println(myVar);
}

// Outputs
LOW
MEDIUM
HIGH

 

标签:MEDIUM,Java,Level,enum,myVar,Enums,constants
From: https://www.cnblogs.com/ShengLiu/p/16928936.html

相关文章

  • Java: Interface
    Anotherwaytoachieve abstraction inJava,iswithinterfaces.An interface isacompletely"abstractclass"thatisusedtogrouprelatedmethodswithem......
  • Java: Inner Classes
    InJava,itisalsopossibletonestclasses(aclasswithinaclass).Thepurposeofnestedclassesistogroupclassesthatbelongtogether,whichmakesyour......
  • Java: Abstraction
    Abstractioncanbeachievedwitheither abstractclasses or interfaces.The abstract keywordisanon-accessmodifier,usedforclassesandmethods:Abst......
  • Java: Classes
    WhatareClassesandObjects?Aclassisatemplateforobjects,andanobjectisaninstanceofaclass.   Staticvs.Publicwecreateda static met......
  • Collectors groupingBy() method in Java with Examples
    https://www.geeksforgeeks.org/collectors-groupingby-method-in-java-with-examples/The groupingBy() methodofCollectorsclassinJavaareusedforgroupingo......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • Stream In Java
    https://www.geeksforgeeks.org/stream-in-java/ IntroducedinJava8,theStreamAPIisusedtoprocesscollectionsofobjects.Astreamisasequenceofobje......
  • Comparable vs Comparator in Java
    https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ Javaprovidestwointerfacestosortobjectsusingdatamembersoftheclass:  Comparabl......
  • equals() and hashCode() methods in Java
    https://www.geeksforgeeks.org/equals-hashcode-methods-java/ Java.lang.objecthastwoveryimportantmethodsdefined:publicbooleanequals(Objectobj)andp......
  • enum in Java
    https://www.geeksforgeeks.org/enum-in-java/Enumerationsservethepurposeofrepresentingagroupofnamedconstantsinaprogramminglanguage.Forexample,t......