I'm not sure if java has this capability, but can someone let me know?
Suppose I have Class A, with subclasses A1 and A2
I want a static variable with the same name for each of the subclasses.
For instance, I would like to have this heirarchy have a static var
called "factory" such that each subclass will have it's own single
instance of it. I know that I can define this static var at each of the
subclass levels (Class A1 and A2), but is there a way to define it in
Class A such that A1 and A2 will have their own? I thought I remember a
special type like this in C#, but could not find anything like this in
java.
Thanks
cdx
Sebastian Scheid - 26 May 2005 12:38 GMT
> I'm not sure if java has this capability, but can someone let me know?
>
[quoted text clipped - 7 lines]
> that A1 and A2 will have their own? I thought I remember a special type
> like this in C#, but could not find anything like this in java.
No, you cannot do that. You have to declare a static variable in each of the
classes if you want each of them to have its own.
In Java you cannot have a baseclass with static elements (like a static
factory method or a static getInstance() method of a singleton) which is
extended by a concrete subclass (e.g. a concrete Singleton) which then has
these static elements of the baseclass for its own.
If you just want to implement the abstract factory design pattern, only your
abstract baseclass (A) needs the factory field (an instance of the concrete
subclass A1 or A2). Your clients will always use A.getFactory() to get the
right implementation and do not know anything about A1 or A2.
Regards
Sebastian