dcsimg
Why they make it "Final"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Friday, June 13, 2003 03:44 AM

Hi Below is a code for creating singleton class public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor supresses // default public constructor private Singleton( ) { } public static Singleton getInstance( ) { return INSTANCE; } } If you I would like to know why did they make the "INSTNNCE" as final. Does static alone serve the fuction? thanks sabu    More>>

Hi


Below is a code for creating singleton class


			

public class Singleton {
private static final Singleton INSTANCE =
new Singleton();

// Private constructor supresses
// default public constructor
private Singleton( ) {
}

public static Singleton getInstance( ) {
return INSTANCE;
}
}

If you


I would like to know why did they make the "INSTNNCE" as final. Does static alone serve the fuction?

thanks

sabu

   <<Less

Re: Why they make it "Final"

Posted By:   Adrian_K  
Posted On:   Friday, June 13, 2003 04:24 AM

This is Sigleton patern implementation....The clas constructor is declared private...so you can't invoke directly...In order to get an instance of the class you may invoke the 'getInstance()' metod...this method internally calls the constructor an returns to you, a Singleton instance...

Static mens that this variable will be shared along all running instances of Singleton classes

In this case you will have running an only instance of this class.
About | Sitemap | Contact