Java: Parent Methods accessing Subclasses' static variables?

假如想象 提交于 2019-12-24 00:59:16

问题


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 the Parent accessing these variables.



Any idea how can this be done ?


回答1:


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; }
}



回答2:


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.




回答3:


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.




回答4:


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).



来源:https://stackoverflow.com/questions/5394712/java-parent-methods-accessing-subclasses-static-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!