Hiding Static Variables

I’ve got several classes of game object that are all descended from one basic class. I want each class to maintain its own static Hashtable, which will contain
every initialized instance of that class.

To this end, I declare a private static Hashtable objectTable in each class, and init them, in each class, in a static block.

Trouble is, all my classes always end up accessing the table for that basic class whenever I try to iaccess the table directly (ie - objectTable.put(…) or something). I’ve developed a very ugly workaround, wherein I place the exact same method:

Hashtable getObjectTable() {return objectTable;}

in every subclass, and always use getObjectTable().put(…) instead of just objectTable.put(…). Now the correct static table is always used. Why is this necessary? Am I doing something wrong or is this just how static hiding works? Thanks all,

Bret

1.4.2, Windows XP BTW :-[

Since it should not be possible to access private members of a base class from a derived class, I would have to say this is a bug.

You should file a bug report.

Augh, yes, I must have changed it from protected at some point and forgotten that’s how it originally was.

Thanks for the reply… I found this on the Sun Forums in case anyone else has this problem. The short answer is, yes, that’s how it works. I can’t think of why it should work that way, but what the hey.

http://forum.java.sun.com/thread.jsp?forum=31&thread=484011

Ah, I think I may have misunderstood your original problem.

If it was base-class methods that were accessing the hashtable then it makes sense.

The two ways to solve the problem are the way you did it with an accessor method to get the hashtable, or to use ‘put’/‘get’ methods in the derived classes that then access the hashtable methods directly, so the code reads a little nicer.