• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

customized constructors

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what would i need to do if only one instance of a class should be produced?
anyone can use only one object..(Question asked in HF java book)

soln:
This is sample class code snippet.Please say whether it is correct for the above question...
public class firstclass
{
static count=0;

firstclass()
{
if (count<=0){
system.out.println("object created");
count++;
}

}
}
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi selvakumar ! I think the question is about singleton pattern. The solution would look like this

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be a good idea to to make the getInstance() synchronized.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you have a private static Singleton instance = new Singleton(); field. Then you can return instance from the getInstance() method.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Or you have a private static Singleton instance = new Singleton(); field. Then you can return instance from the getInstance() method.



There's a reason to why I didn't write that. Once I wrote that and someone got confused and said that the code must result in a StackOverflowError. He was confused about instantiating instance fields of a class in itself . If someone is confused in which case the StackOverflowError will occur, this program will generate that error

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was whoever told you off who was in error; as you will see, you only get a stack overflow if the instance field is not static.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It was whoever told you off who was in error; as you will see, you only get a stack overflow if the instance field is not static.



Yes I know that. I was just saying that I didn't wrote the program with direct instantiation of the static field because it tends to confuse some people. What you are saying is 100% correct and I agree with you ...
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to educate your colleague; that is one of the ways to implement a Singleton recommended by Joshua Bloch in Effective Java 2/e page 17-18. As I said, it is the other person who is mistaken about this.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:As I said, it is the other person who is mistaken about this.



Ohh, I thought that you are thinking that I am confused with that. Actually I was confused about what you said about people being confused because I thought that you are thinking that I am confused (now that's a confusing statement )
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And are you more confused or less confused about it now?
 
reply
    Bookmark Topic Watch Topic
  • New Topic