The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Problems with Singletons and Static References

Posted by Matt Gerrans on February 22, 2002 at 1:59 PM

Hmm... actually this.manager = this; seems to compile and work okay even though it doesn't really make any sense and certainly makes for confusing code. I guess it follows the same idea that you can use any instance to access a static method (which some people think should not be the case), but if I wrote the compiler, it would be an error, or at the very least, a warning.

> I'm trying to implement a class that uses a static method to return an instance of itself to the calling object. It's not precisely a singleton because it can also be instantiated via a call to a constructor.

> I've gotten it to work more or less correctly, but I'm having trouble with methods in the instance that access non-static variables:

> The pertinent code follows:

> public class GeneManager {

> private GeneManagerGUI gui;

> private static GeneManager manager;
> private static boolean initialized = false;

> public GeneManager() {
> this.manager = this;
> initialized = true;
> }


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us