tags:

views:

450

answers:

1

In .NET you can achieve something like this:

Color yellowColor = Color.FromName("yellow");

Is there a way of doing this in Java without having to resort to reflection?

PS: I am not asking for alternative ways of storing/loading colors. I just want to know wherever it is possible to do this or not.

+2  A: 

Not sure if this is considered Reflection,

Color color;
try {
    Field field = Class.forName("java.awt.Color").getField("yellow");
    color = (Color)field.get(null);
} catch (Exception e) {
    color = null; // Not defined
}
ZZ Coder
It is reflection and it is the only solution I can think of. Any other way would require creating a map of colors.
tulskiy
It is reflection but that is welcome. It seems to be the only way to do it...I wonder why does everything in Java have to be so complicated..
devoured elysium
@devoured elysium: After using ruby for some time, I don't like java because you have to reinvent the wheel many times. You can use groovy, it allows you to access fields by the name.
tulskiy