--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
Thanks a lot
If you do not need lazy loading of the singleton, use directly:
public class ChatRoom {
private static final ChatRoom instance = new ChatRoom();
public static ChatRoom get() { return instance; }
}
If you need lazy loading, you can use an enum for this
public enum ChatRoom {
INSTANCE;
public static ChatRoom get() { return INSTANCE; }
// you can use the constructor for lazy loading
}
Do not forget that you have one singleton pro JVM.
To be more precise, you have one singleton pro classloader.
If 2 classloaders load the singleton, you will have 2 instances.
For more information, read:
http://java.sun.com/developer/technicalArticles/Programming/singletons/
Citation:
" Multiple Singletons Simultaneously Loaded by Different Class Loaders
When two class loaders load a class, you actually have two copies
of the class, and each one can have its own Singleton instance. That
is particularly relevant in servlets running in certain servlet
engines (iPlanet for example), where each servlet by default uses its
own class loader. Two different servlets accessing a joint Singleton
will, in fact, get two different objects.
Multiple class loaders occur more commonly than you might think.
When browsers load classes from the network for use by applets, they
use a separate class loader for each server address. Similarly, Jini
and RMI systems may use a separate class loader for the different code
bases from which they download class files. If your own system uses
custom class loaders, all the same issues may arise.
If loaded by different class loaders, two classes with the same
name, even the same package name, are treated as distinct -- even if,
in fact, they are byte-for-byte the same class. The different class
loaders represent different namespaces that distinguish classes (even
though the classes' names are the same), so that the two MySingleton
classes are in fact distinct. (See "Class Loaders as a Namespace
Mechanism" in Resources.) Since two Singleton objects belong to two
classes of the same name, it will appear at first glance that there
are two Singleton objects of the same class."
Yann
2011/8/5 Karim Heraud <khe...@gmail.com>: