Take the 2-minute tour ×
public Car{

public enum Color{RED,BLUE};
private Color color;

Car(Car.Color c)
this.color =c



is it the correct way?

share|improve this question

5 Answers 5

Its correct. Although I'd keep the enum in a separate file Color.java, because its quite generic.

share|improve this answer
I would also rename the enum to avoid conflicts with java.awt.Color –  Carlos Heuberger Aug 18 '10 at 8:56

Looks ok.

What other options do you have?

share|improve this answer

Yep, that's ok. you can expose a getter to the private member as well. So other classes could see the car's color.

share|improve this answer
It's quite possible that the color doesn't need to be exposed. Making the enum public so that any caller can pass an instance into the constructor still makes sense. In general, the less information that's arbitrarly exposed the more robust the class design is. –  Andrzej Doyle Aug 18 '10 at 7:06

Jep, looks good.

share|improve this answer

You can put an enum inside a class, just like you can create another class inside a class (that would be a nested class, and if it's not static it's an inner class).

share|improve this answer

Your Answer


