Declare enum variable in Java bean

I need to declare the enum variable as a member of the class and you need to define the setter and getter for this, as a java bean. something like that -

public class Vehicle {
 private String id;
 private String name;
 enum color {
   RED, GREEN, ANY;
 }
 // setter and getters
} 

Now I want the color property to be red, green, or any other class and want to make the appropriate decisions.

+5
source share
2 answers

The listing should be open to the outside world:

public class Vehicle {
     private String id;
     private String name;

     public enum Color {
       RED, GREEN, ANY;
     };

     private Color color;    

     public Color getColor(){
        return color; 
     }

     public void setColor(Color color){
         this.color = color;
     }   

    } 

Then from the package side you can do:

vehicle.setColor(Vehicle.Color.GREEN);

if you only need to use Vehicle.Color inside the same package as Vehicleyou can remove publicfrom the ad enum.

+13
source

color, , . public enum color .

. color color, . : private Color color .

, , . , :

Vehicle.Color myColor = Vehicle.Color.RED;

Bakkal , , . . !

+2

All Articles