my dog learned polymorphism*
The moose likes Beginning Java and the fly likes About Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Singleton Pattern" Watch "About Singleton Pattern" New topic
Author

About Singleton Pattern

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi there,
well i was just going through the code for singleton pattern. In that code i got that constructor was private. And that is my problem. In simple sense, what java says that if you consider access specifier, you can widen, not narrowing. Now if we do not provide any constructor to a class, java simply provides a default constructor, and we can easily instantiate the class outside of that class, which proves that default constructor is not private. So in case of singleton pattern how we can do narrowing?
Please help me out.

Thanks in advance.
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

In the singleton pattern a constructor is still provide but it is private.

The constructor to a singleton is generally access through a static method, and in that static method you check to see if an instance of that class has been created, if it has been created you return the instance, if it hasn't been created you create a new one.

For example


Conceptually the sticking point for me is that static (class) methods are able to be called without and instance, and you use a static method to check to see if an instance exists.

I find it a little convoluted, but it works for me


when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

If you are actually talking about narrowing a constructor, a singleton class constructor that is private has to be explicitly defined to make it a singleton class.
So there is no default constructor provided as we already have a constructor.

We need to have a private constructor and a single instance of the class as instance variable of the class to make it a singleton class.

Am i making myself clear?


-kolli
Padma Latha
Greenhorn

Joined: Oct 05, 2008
Posts: 1
hi sidiqui.
yes you were right when you said that compiler will generate the default constructor, if you dont provide one. But I got JLS extract for you to help clear the contention:




8.8.10 Preventing Instantiation of a Class
A class can be designed to prevent code outside the class declaration from creating instances of the class by declaring at least one constructor, to prevent the creation of an implicit constructor, and declaring all constructors to be private. A public class can likewise prevent the creation of instances outside its package by declaring at least one constructor, to prevent creation of a default constructor with public access, and declaring no constructor that is public.

Thus, in the example:



the class ClassOnly cannot be instantiated, while in the example:



the class PackageOnly can be instantiated only within the package just, in which it is declared.

Hope it clears your question.
Padma.

Originally posted by kayanaat sidiqui:
Hi there,
well i was just going through the code for singleton pattern. In that code i got that constructor was private. And that is my problem. In simple sense, what java says that if you consider access specifier, you can widen, not narrowing. Now if we do not provide any constructor to a class, java simply provides a default constructor, and we can easily instantiate the class outside of that class, which proves that default constructor is not private. So in case of singleton pattern how we can do narrowing?
Please help me out.

Thanks in advance.
[edit]Add code tags. CR[/edit]
[ October 05, 2008: Message edited by: Campbell Ritchie ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, the private constructor in a Singleton is not an example of narrowing.

In fact, a constructor is *never* an example of neither widening nor narrowing. That is because constructors aren't inherited and therefore also can't be overridden.

The reason that you can't narrow the access to an instance method is that it would mess up the static type system:



That last line is a problem, because the compiler has to allow the access to the foo() method. To ensure compile time consistency, SubFoo is not allowed to make access more restricted, so this leads to a compile time error.

This conflict simply can't happen with constructors, because they are never called polymorphically.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: About Singleton Pattern