Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting static variable from abstract class

I have half a dozen classes which all extend the same abstract class. The abstract class has a static variable pointing to some JNI code that I only want to load once per instantiation of the classes.

From what I understand this results in exactly one instance of this static variable being instantiated, but what I want is for each of the extending classes to have their own static instance of the variable that is unique for the given child class. I want to write some code in my abstract class that modifies and/or releases the abstract class. Is it possible to do both of these things at once?

So as an example can I write an abstract class bar with an variable foo and a printFoo method which prints the content of foo. Then I instantiate in order fooBar1, fooBar2, and fooBar3 which each extend the bar class and initialize foo to different values in static blocks. If I call foobar1.printFoo I want to print the static value of foo initialized by fooBar1 constructor.

Can this be done in java?

like image 534
drew Avatar asked Apr 14 '11 18:04

drew


People also ask

Can abstract class have static variables?

Type of variables: Abstract class can have final, non-final, static and non-static variables.

Can static class inherit abstract class?

NO, we can not inherit static class in c# because they are sealed and abstract. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. Any class which does not contain instance contructor, cannot be inherited.

Can static variables be inherited?

Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. We can inherit static methods in Java.

Can I inherit from an abstract class?

An abstract class cannot be inherited by structures. It can contain constructors or destructors.


1 Answers

You can approximate it, but you will need separate static variables for each subclass, to stop subclasses overwriting each others values. It's easiest to abstract this via a getter getFoo so that each subclass fetches the foo from the right place.

Something like this

abstract class Bar
{
   // you don't have to have this in the base class 
   // - you could leave out the variable and make
   // getFoo() abstract.
   static private String foo;

   String getFoo() {
     return foo;
   }

   public void printFoo() {
      System.out.print(getFoo());
   }
}

class Foo1 extends Bar
{
   static final String foo1;

   public String getFoo() {
      return foo1;  // return our foo1 value
   }

   public Foo1() {
      foo1 = "myfoo1";
   }
}


class Foo2 extends Foo1
{
   static final String foo2;

   public String getFoo() {
      return foo2;  // return our foo2 value
   }

   public Foo2() {
      foo2 = "myfoo2";
   }
}
like image 177
mdma Avatar answered Oct 02 '22 18:10

mdma