4 Replies Latest reply: Dec 6, 2008 11:55 PM by 807589 RSS

    static variables and inheritance

      Here's what I'm trying to accomplish.

      I have a bunch of behaviors whose superclass is AbstractBehavior. I would like the user to be able to enter values into textfields to populate certain parameters of the constructor. To facilitate this I'd like to provide labels for these textfields and I'd like the labels to be part of the behavior objects themselves.

      First I thought of having a static array of strings that contained all of the labels. The problem is that if I have an AbstractBehavior, I want polymorphism to choose which array is used. For example, I have class Control extends AbstractBehavior and if I have code like this:
      AbstractBehavior ab = getBehavior();//Returns abstractbehavior
      for(String s:ab.PARAMS)
      It won't work. I imagine this is because polymorphism doesn't apply to public static final variables?

      My next thought was to just make it an instance variable and return it with a getParams() method but that requires me to instantiate the object, and I'd rather not have to instantiate an object just to get something that should be static.

      Any thoughts?
        • 1. Re: static variables and inheritance
          Declare getParams() as abstract method in AbstractBehaviour and let the implementor return the statically declared params through it.
          • 2. Re: static variables and inheritance
            yea but then I have to instantiate the object no?
            • 3. Re: static variables and inheritance
              JFactor2004 wrote:
              yea but then I have to instantiate the object no?
              huh? how are you going to use any object without instantiating it first? perhaps you need to explain yourself further?
              • 4. Re: static variables and inheritance
                Well if I had a static variable I could access it by saying ClassName.variableName without instantiating a type of that class right? Basically I want to access a static variable by reflection.

                If I have to create an instance then I wind up doing something like this:
                Object ab = ct.newInstance(new Object[formals.length]);
                Because I have to make sure to get the right instructor
                and then in my code I have to do things like this:
                public Control(String left,String up, String right, String down)
                        myParams = new String[4];
                            myOriginalLeft = left.equals("true");
                            myOriginalUp = up.equals("true");
                            myOriginalRight = right.equals("true");
                            myOriginalDown = down.equals("true");
                        catch(NullPointerException e)
                            System.out.println("Fake instantiation");
                to differentiate between meaningful and non meaningful instantiations

                Edited by: JFactor2004 on Dec 7, 2008 12:53 AM