• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

About Singleton Pattern

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 239
2
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch.

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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic