Doubt about Singleton Implementation

Hello i have seen 2 different implementations for making a class singleton

First is

publicclass A{

privatestatic A INSTANCE =null;

private A(){}

publicstatic getInstance(){

if(INSTANCE ==null){

INSTANCE =new A();

}

return INSTANCE;

}

other is

publicclass A{

privatestatic inst =null;

private A(){}

publicstatic A getInstance()

{

if(inst ==null)

{

synchronized(A.class)

{

if(inst ==null)

inst =new A();

}

}

return inst;

}

}

What is the difference

[1982 byte] By [hi_alla] at [2007-10-3 10:23:19]
# 1

not much. somebody is about to tell you that the second one is better, and waffle on about thread safety or something. don't listen to them. neither is thread-safe

unless your singleton object is expensive to create, you're as well doing this:

public class A {

private static A INSTANCE = new A();

private A(){}

public static getInstance(){

return INSTANCE;

}

}

georgemca at 2007-7-15 5:45:02 > top of Java-index,Java Essentials,Java Programming...
# 2
2nd choice is better why in the sense. there they are making a lock on that object. using synchronized block.single instance reference can be getting somewhere elsei hope u got.
narayanab16a at 2007-7-15 5:45:02 > top of Java-index,Java Essentials,Java Programming...
# 3
> 2nd choice is better > > why in the sense. > there they are making a lock on that object. using> synchronized block.> single instance reference can be getting somewhere> else> i hope u got.told you
georgemca at 2007-7-15 5:45:02 > top of Java-index,Java Essentials,Java Programming...