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
mike_kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mike_kAuthor Commented:
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?
mike_kAuthor Commented:
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?

sciuriwareCommented:
There should be a solution by means of reflection here.

;JOOP!
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

CEHJCommented:
You could try something like this (save and compile as NameConstants.java):



import java.util.*;

public class NameConstants {
      public final static String NAME_A = "name_a";
      public final static String NAME_B = "name_b";
      
      // Testing only
      public static void main(String[] args) {
            MyNames mn = new MyNames();
            YourNames yn = new YourNames();
            testNames(mn);
            testNames(yn);
      }
      
      static void testNames(Names names) {
            System.out.println(names.getName("NAME_A"));
      }
}

abstract class Names {
      protected Map names;

      public String getName(String name) {
            return (String)names.get(name);
      }
}


class MyNames extends Names {
      
      
      public MyNames() {
            names = new HashMap();            
            names.put("NAME_A", NameConstants.NAME_A + ".my");
            names.put("NAME_B", NameConstants.NAME_B + ".my");
      }
      
}

class YourNames extends Names {
      
      public YourNames() {
            names = new HashMap();            
            names.put("NAME_A", NameConstants.NAME_A + ".your");
            names.put("NAME_B", NameConstants.NAME_B + ".your");
      }
      
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mike_kAuthor Commented:
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
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.