开发者

Java: Parent Methods accessing Subclasses' static variables?

I am trying to understand my way around polymorphism in Java. I created a parent class that has too many common methods that all children will use in the same manner.

Each of the subclasses' children all share static information, These variables or information will be used in the methods declared only in the parent.

The problem wish accessing static variables from Parent methods seems not really possible,

Its a solution to declare the common information per instance but since there will be 1000s of instances its such a waste of memory.

A simple elaboration of what i mean is the following code :

    class testParent {
    static int k;
    public void print()
    {
      System.out.println(k);    
    }
   }

   class testChild2 extends testParent 
   {

    static
    {
        testChild2.k =2;
    }
   }

   public class testChild1 extends testParent{
    static
    {
        testChild1.k = 1;
    }

    public static void main(String[] args)
    {

        new testChild1().print();
        new testChild2().print();

        new testChild1().print();
    }
 }

the output i expect was

1

2

1.

but what happens is :

1

2

2

One might think that on the initiation of each subclass the static variables of this subclass is set and then all methods referring to this subclass has access to the corresponding 'k' value.

But what actually happens is that all subclasses edit in the same static variable that is shared along all subclasses and hence destroys my whole point of using static variables for each subclass and its instances and using commmon methods in t开发者_JS百科he Parent accessing these variables.

Any idea how can this be done ?


An option is to access the subclasses' static data through an abstract (non-static) method:

abstract public class Parent {
   protected abstract int getK();

   public void print() {
      System.out.println(getK());
   }
} 

public class Child1 extends Parent {
   private static final int CHILD1_K = 1;

   protected int getK() { return CHILD1_K; }
}

public class Child2 extends Parent {
   private static final int CHILD2_K = 2;

   protected int getK() { return CHILD2_K; }
}


When you make new testChild2().print(); the static block on testChield2 was executed and change the value to 2.

static blocks only execute once when loaded by the ClassLoader.

This one give the output you want:

 class testParent {
    static int k;
    public void print()
    {
      System.out.println(k);    
    }
   }

   class testChild2 extends testParent 
   {

    {
        testChild2.k =2;
    }
   }

   public class testChild1 extends testParent{
    {
        testChild1.k = 1;
    }

    public static void main(String[] args)
    {

        new testChild1().print();
        new testChild2().print();

        new testChild1().print();
    }
 }

Non static code blocks execute everytime the class is instanciated.


Premature optimization is the root of all evil. I don't think you'll run into any memory issues with thousands of instances, each with their own data, unless you're working on a tiny embedded system of some kind. Static variables are not intended to do what you're trying to do with them.


Static variables are specific to the class itself. If you want the same field in different instances of a class to have different values, then that field cannot be static.

The solution: don't make k static.

class testParent {
    int k;
    public void print()
    {
        System.out.println(k);    
    }
}

class testChild2 extends testParent 
{
    {
        this.k =2;
    }
}

class testChild1 extends testParent{
    {
        this.k = 1;
    }

    public static void main(String[] args){
        new testChild1().print();
        new testChild2().print();
        new testChild1().print();
    }
}

Demo
(ignore the static class business - that's just to make it work in ideone).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜