ako
ako

Reputation: 117

using enum in a class?

public Car{

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

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

}


}

is it the correct way?

Upvotes: 2

Views: 518

Answers (5)

Jesper
Jesper

Reputation: 206776

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).

Upvotes: 0

naikus
naikus

Reputation: 24472

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

Upvotes: 4

tobiasbayer
tobiasbayer

Reputation: 10369

Jep, looks good.

Upvotes: 1

Adibe7
Adibe7

Reputation: 3539

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

Upvotes: 1

helios
helios

Reputation: 13841

Looks ok.

What other options do you have?

Upvotes: 1

Related Questions