Link to home
Start Free TrialLog in
Avatar of mike_k
mike_k

asked on

how can I type a class full of static members

I want to be able to pass a class full of static members as a parameter.  I want to do this to select namespaces to set up on another class.

For example, I might have two classes:

public class MyNames{
public static final NAME_A= "nameA";
public static final NAME_B= "nameB";
public static final NAME_C= "nameC";
}

public class YourNames{
public static final NAME_A= "nameA.you";
public static final NAME_B= "nameB.you";
public static final NAME_C= "nameC.you";
}

and want to use one or the other in a constructor for another class:

public class NamingClass{
public NamingClass(NameConstants names){
}

public doSomethingWithNameA(){ }
public doSomethingWithNameB(){ }
public doSomethingWithNameC(){ }
}

the goal being that I could then subclass NamingClass with YourNamingClass to behave like NamingClass using YourNames while leaving MyNamingClass available to use MyNames.

How do I create a type for MyNames and YourNames so that I can pass an instance to the naming class?  I tried creating a superclass
public class NameConstants{
public static final NAME_A;
public static final NAME_B;
public static final NAME_C;
}
which is no good since the static is on NameConstants instead of the subclass where it belongs.

I would really like MyNames and YourNames to remain full of constants as they will be used in a configuration servlet.

I am thinking that this is a problem that must have been solved before, so if you've seen it, please help.



Thanks
Avatar of mike_k
mike_k

ASKER

It looks like what I ask for is not possible.  This is because even if I could do it the superclass would be the class evaluating the static members, and they would not be properly defined on the superclass.
my reasoning is based along the lines of this: http://zoom.z3950.org/bind/java/comment/static.html


Is there a reasonable way to accomplish something similar?
Avatar of mike_k

ASKER

Sometimes creating the quetion helps to define it's solution.

public interface NamingClass{
      public String getNameA();
      public String getNameB();
      public String getNameC();                        
}

public class MyNames{
public static final NAME_A= "nameA";
public static final NAME_B= "nameB";
public static final NAME_C= "nameC";

public String getNameA(){
return NAME_A;
}
.
.
.
}

This lets me keep the contants for use elsewhere but still get at them through a parameter call.

Is there any reason that this is a very bad idea?

There should be a solution by means of reflection here.

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mike_k

ASKER

CEHJ,
Sorry to take so long to respond, I am a rather slow study and had other stuff slowing me down even more.

What you suggest works perfect.  It allows extension of namespaces as well as simple overriding of them.

Thanks,
Mike
:-)