[4722] in java-interest

home help back first fref pref prev next nref lref last post

HELP - need abstract static variables (no good solution?)

daemon@ATHENA.MIT.EDU (Ran Levy - Software)
Thu Jan 11 16:07:59 1996

Date: Thu, 11 Jan 1996 05:20:23 +0200
From: ranlevy@eandm.co.il (Ran Levy - Software)
To: java-interest@java.Eng.Sun.COM

Hi all,

Can anybody think of a good solution for the following scenario, or maybe
explain the rational behind prohibiting abstract static variables in Java??

Static variables declared in some Java class cannot be declared abstract.

The following scenario cannot be solved without redundant duplication of
part of the code, because of Java (artificial?) limitation.

I have a class (GeneralClass) that defines general methods for a group
of other classes derived from it (I omit the constructors in this example):

	public class GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  void printData () {
	    // print values from dataVar, uses infoVar
	  }

	  int [] readData (String filename) {
	    // also modifies the variable infoVar
	  }
	}

	public class SubClassOne extends GeneralClass {
	}

	public class SubClassTwo extends GeneralClass {
	}

The problem is that in this code all instances of GeneralClass,
SubClassOne, and SubClassTwo share the same value of dataVar. However,
according to the required design, SubClassOne and SubClassTwo should
only differ by the value of dataVar. Also, the dataVar value MUST be
common for all instances of SubClassOne, and I'd therefore like to
initialize it in a static block of code.

Notice that the code to read the data from the file is common to all
subclasses, and therefore resides in GeneralClass.

What I'd actually want to do is:

	public class GeneralClass {
	  static abstract int [] dataVar = null; // problem: illegal in Java
	  static abstract int infoVar = null;    // problem: illegal in Java

	  void printData () {
	    // print values from dataVar, uses infoVar
	  }

	  int [] readData (String filename) {
	    // also modifies the variable infoVar
	  }
	}


	public class SubClassOne extends GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  static {
	    dataVar = readData ("filename"); // problem: readData is not static
	  }
	}

OK, this is illegal. I cannot use "static abstract" variables.
I cannot omit the declaration of dataVar and infoVar from GeneralClass,
because printData() and readData() would not compile. So I keep them
static, and overload them in the subclasses. I also correct the error
and make readData static.

	public class GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  void printData () {
	    // print values from dataVar, uses infoVar
	  }

	  static int [] readData (String filename) {
	    // also modifies the variable infoVar
	  }
	}


	public class SubClassOne extends GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  static {
	    dataVar = readData ("filename");
	  }
	}

Hmmm - the problem now is that although the data is read, the invocation of
subClassOne.printData () causes a reference to the static variable GeneralClass.dataVar rather than subClassOne.dataVar. Also, the code
for readData modifies the wrong infoVar static variable.

What I ended up with was a complex structure of 4 abstract methods of
GeneralClass, which are implemented in SubClassOne and SubClassTwo
(and all other subclasses of GeneralClass), all saying the same thing:

	public class SubClassOne extends GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  final public int [] getData () {
	    return dataVar;
	  }


	  final public void setData (int [] data) {
	    dataVar = data;
	  }

 	  final public int getInfo () {
	    return infoVar;
	  }


	  final public void setInfo (int info) {
	    infoVar = info;
	  }

The method readData of GeneralClass is no longer static, becuase I want the
corrent infoVar (that one of SubClassOne or SubClassTwo) to be modified.
I therefore cannot use the static block to initialize dataVar in SubClassOne.
The final version of SubClassOne includes an ugly patch like this:


	public class SubClassOne extends GeneralClass {
	  static int [] dataVar = null;
	  static int infoVar = null;

	  private void initData () {
	    if (dataVar != null) 
	      dataVar = readData ("filename");
	  }

	  SubClassOne () {
	    super ();
	    initData ();
	  }

	  final public int [] getData () {
	    return dataVar;
	  }


	  final public void setData (int [] data) {
	    dataVar = data;
	  }

 	  final public int getInfo () {
	    return infoVar;
	  }


	  final public void setInfo (int info) {
	    infoVar = info;
	  }
	}

And I have to re-implement initData(), getData(), setDate(),
getinfo(), and setInfo () in exactly the same way in EVERY subclass
of GeneralClass, actually redundantly REPEATING the same code for each
one subclass. Which is opposed to the object-oriented / inheritance
paradigm, isn't it?

I find this to be mostly non-elegant solution, but I found no other
workaround for this "language bug" (of forbidding abstract declaration
of variables).

Comments, please.

Thanks, 
-- Ran.

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post