I’d like to be able to declare a Java class Ref such that I could, elsewhere in code, do things like this:
switch (v)
{
case Ref.LicenseCode: ....;
case Ref.Widget.MaxWeight: ....;
case Ref.Widget.MolyBolt.ThreadsPerInch: ....;
}
Ref is intended to be constant data structure representing a hierarchical set of constant values, such as often appears in standards documents or other reference material. I want values that are truly constant (so they can be used in a case statement).
I thought I might be able to do this by nesting class definitions, and it works… to a point. For example this:
public final class Ref
{
public final static int LicenseCode = 800;
public final class Widget
{
public final static int MaxWeight = 5000;
}
}
lets me write this:
switch (v)
{
case Ref.LicenseCode: ....;
case Ref.Widget.MaxWeight: ....;
}
but when I try to nest down to the third level:
public final class Ref
{
public final static int LicenseCode = 800;
public final class Widget
{
public final static int MaxWeight = 5000;
public final class MolyBolt
{
public final static int ThreadsPerInch = 12;
}
}
}
I am told that:
"Ref.Widget.MolyBolt cannot be resolved or is not a field."
Am I doing something wrong? Or have I bumped up against one of the edges of Java? Is there some other way to accomplish my goal? I am running under Windows Vista, JCK 1.6.0-21, using Eclipse Java Development Tools 3.5.2.r352.
How are you referencing the
ThreadsPerInchfield? This works for me:The only thing I’d change is to make inner classes static, though you’re probably not going to instantiate any objects of this class anyway.