Advertisement
yo2man

Tutorial 43 Enum Types: Basic and Advanced Usage

May 25th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. //Tutorial 43 Enum Types: Basic and Advanced Usage
  2. //Enums are lists of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum.
  3. //For instance, in a chess game you could represent the different types of pieces as an enum:
  4. /* 
  5. enum ChessPiece {
  6. PAWN,
  7. ROOK,
  8. KNIGHT,
  9. BISHOP,
  10. QUEEN,
  11. KING;
  12. }
  13. */
  14.  
  15. //---------------------------------------------------------------------------------------------------------------------------------------
  16. //App.java
  17. public class App {
  18.  
  19.    
  20.     public static void main(String[] args) {
  21.        
  22.         Animal animal = Animal.CAT;
  23.         switch(animal) { //click on the warning>"add missing case statements" to add cases
  24.         case CAT:
  25.             System.out.println("CAT");
  26.             break;
  27.         case DOG:
  28.             System.out.println("DOG");
  29.             break;
  30.         case MOUSE:
  31.             break;
  32.         }
  33.         //Let's look at more advance uses of enums:
  34.         System.out.println(Animal.DOG);
  35.        
  36.         System.out.println(Animal.DOG.getClass());
  37.        
  38.         System.out.println(Animal.DOG instanceof Enum); //"instanceof" operator tells us if the object is an instance of(created from a particular class) the class(true or false). in this case when you run it, it should be: true
  39.        
  40.         //3*
  41.         System.out.println(Animal.MOUSE.getName());
  42.        
  43.         System.out.println("Enum name as a string: " + Animal.DOG.name()); //get actual name of Enum
  44.  
  45.        
  46.         Animal animal2 = Animal.valueOf("CAT"); //use static method "valueOf" to get name of the Enum constants //in this instance it should say "Fergus" when you run it
  47.             System.out.println(animal2);
  48.        
  49.     }
  50.  
  51. }
  52.  
  53. //---------------------------------------------------------------------------------------------------------------------------------------
  54. //Animal enum
  55. //rightclick on default package>new>Enum:
  56. public enum Animal {
  57.     //CAT, DOG, MOUSE
  58.     // 3* You can give enum types a constructor
  59.     CAT("Fergus"), DOG("Fido"), MOUSE("Jerry");
  60.    
  61.     private String name;
  62.     Animal(String name){
  63.         this.name = name;
  64.        
  65.     }
  66.     public String getName() { //source>GettersandSetters>check name field
  67.         return name;
  68.     }
  69.     public void setName(String name) {
  70.         this.name = name;
  71.     }
  72.  
  73.     public String toString(){
  74.         return "This animal is called: " + name;
  75.     }
  76. }
  77.  
  78. //---------------------------------------------------------------------------------------------------------------------------------------
  79. /* Run results:
  80. CAT
  81. This animal is called Fido
  82. class Animal
  83. true
  84. Jerry
  85. Enum name as a string: DOG
  86. This animal is called Fergus
  87. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement