Could Some one detail me on that famous "getInstance()" that i lost Days ago !
Discussion - the powerfull way to excellence!
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Originally posted by Akhilesh Trivedi:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
Discussion - the powerfull way to excellence!
Originally posted by prashant bhardwaj:
You are quite emotional !
getInstance() is a method most often used with the Singleton pattern.
It is commonly used to get a single instance of a class within a JVM.
It is not a method defined in the core java classes, so no wonder you can't find it there.
Discussion - the powerfull way to excellence!
Originally posted by Eloy Kul:
class MyClass {
static private MyClass This;
private MyClass() {....}
static public MyClass getInstance() {
if(This == null) {
This = new MyClass();
}
return This;
}
public void doStuff() {}
}
MyClass.getInstance().doStuff();
[ September 18, 2008: Message edited by: Eloy Kul ]
Discussion - the powerfull way to excellence!
Originally posted by Campbell Ritchie:
You should improve that by not calling the instance "This" because of the potential for confusion with the keyword this.
Also lose the "!= null" test. Declare and initialise the instance in one statement
. . .
private final MyClass instance = new MyClass();
. . .
and you can simply return it from the getInstance() method:And only a few graemlins please; I'll have to wear sunglasses otherwise.
Discussion - the powerfull way to excellence!
Originally posted by Campbell Ritchie:
By the way, the static modifier is needed or access will be impossible.
Discussion - the powerfull way to excellence!
Originally posted by Campbell Ritchie:
You're welcome
Discussion - the powerfull way to excellence!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |