E
Eric Sosman
Maybe someone can come up with an SCSE where a singleton is needed.
Runtime.getRuntime().exit(0);
Maybe someone can come up with an SCSE where a singleton is needed.
Eric Sosman said:Runtime.getRuntime().exit(0);
Other have.
GoF has it.
Spring has had it since 1.x.
EJB got it in 3.1.
Implementations and usage are very different, but the idea of
everybody using the same object is the same.
Arne
The library (Java SE) could have been defined to allow:
class Main
{ public static void main( final java.lang.Runtime runtime )
{ runtime.println( runtime.getArgc() + " command-line arguments." );
runtime.exit(); }}
or - with less changes to the current state of Java - to allow:
Runtime.exit( 0 );
Eric Sosman said:A singleton class can be transformed into an uninstantiable
class having only static methods.
lipska said:OK, I know I'm probably still being ignored because I've upset some
people but can someone please explain to me exactly what a 'static
class' is (WRT Java of course)
Stefan said:Static methods cannot be passed via reference or used to
implement interfaces. Therefore, there is a need for
non-static methods sometimes. But this does not mean a
singleton, I am thinking of a POJO first and foremost
until someone shows me where a POJO does not suffices,
but a singleton is needed.
OK, I know I'm probably still being ignored because I've upset some
people but can someone please explain to me exactly what a 'static
class' is (WRT Java of course)
markspace said:A real static class is a nested class
that has
no (implied) reference to
its enclosing instance.
whose instances have
their enclosing instances.
BTW: A static class has a reference to its enclosing class.
public class Main
{ static class C {}
public static void main( final java.lang.String args[] )
{ java.lang.System.out.println( C.class.getEnclosingClass() ); }}
I think the real anti-pattern is the common implementation of how to get
the value of the singleton. Singleton's are useful, but when the
singleton status of an object is enforced beyond reason, you end up with
with all kinds of "work-arounds" to the fact that the object is a
singleton. Dependency Injection can help alleviate some of those
problems, by making the singleton nature of the object a consequence of
it only being instantiated by the framework, rather than by the class
itself being a "singleton class".
I think the real anti-pattern is the common implementation of how to get
the value of the singleton. Singleton's are useful, but when the
singleton status of an object is enforced beyond reason, you end up with
with all kinds of "work-arounds" to the fact that the object is a
singleton.
Nitpick: "static class" is the wrong term. I figure you mean a "utility class",
or class with only static members.
Static classes certainly can implement interfaces. It's non-instantiable
classes that can't, and classes containing only static methods.
OK, I know I'm probably still being ignored because I've upset some
people but can someone please explain to me exactly what a 'static
class' is (WRT Java of course)
This is illegal
public static class Foo{...
a class with all static methods certainly can implement an interface
This compiles
public class Foo implements Serializable{
public static void Bar(){
}
}
RTFM:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.5.1
The original statement was inaccurate, as you point out.
What he meant must have been something like "except for static methods, which cannot
implement an interface".
A singleton class can be transformed into an uninstantiable
class having only static methods. An uninstantiable class with
only static methods can be transformed into a singleton class.
The two designs are duals: Why should one be deprecated and the
other preferred?
If all-static vs. singleton is the most pressing problem
someone faces, he has an easy life indeed!
Maybe someone can come up with an SCSE where a singleton is needed.
Arne said:I think you can assume he use "static class" in the C# meaning not the
Java meaning.
Wait, isn't this comp.lang. *java* .programmer?
I'm in the wrong newsgroup!
No, we do not assume any such thing.
Needed as in "problem can not be solved without"? I doubt such exist!
Needed as in "a large of programmers prefer it to solve the problem"?
There are many. Connection pools, configuration settings, web app
statistics etc..
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.