Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2005

Tip: Looking for answers? Try searching our database.

static member question

Thread view: 
cdx - 26 May 2005 01:55 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.