开发者

How to declare a constant hierarchical data structure in Java?

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:

开发者_StackOverflow社区
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.


Looks like bill of material information to me. Embedding this in Java classes as static data seems to be terribly rigid to me. It's far more natural to store it in a relational, hierarchical, object, or graph database.

The other problem is that the code to process this will be a spaghetti forest of if/then/else or switch statements to process.

It's hard to overstate just how wrong-headed this appears to be. You might get an answer that will allow you to proceed, but this can only end in grief.


How are you referencing the ThreadsPerInch field? This works for me:

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;
        }
    }
    public static void main(String[] args) {
        int v = Integer.valueOf(args[0]);
        switch (v) {
            case Ref.LicenseCode:
                break;
            case Ref.Widget.MaxWeight:
                break;
            case Ref.Widget.MolyBolt.ThreadsPerInch:
                break;
        }
    }
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜