 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cdx Guest
|
Posted: Thu May 26, 2005 12:55 am Post subject: static member question |
|
|
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
|
|
| Back to top |
|
 |
Sebastian Scheid Guest
|
Posted: Thu May 26, 2005 11:38 am Post subject: Re: static member question |
|
|
"cdx" <jm1 (AT) dicehome (DOT) com> schrieb im Newsbeitrag
news:ccKdnTpPhPmKgwjfRVn-pQ (AT) comcast (DOT) com...
| Quote: | 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.
|
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|