• Post Reply Bookmark Topic Watch Topic
  • New Topic

create only one instance  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,my name is rohit and i am new to javaranch.I was reading head first java book and saw a barbell question on page no. 280,question-"what if you want to write a class in such a way that only one instance of it can be created,and anyone who wants to use an instance of the class will always use that one,single instance?"

i need help with this question.Thank you very much in advance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search for Singleton or singleton pattern (either on this site or the web in general) you should find plenty of discussions of this.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you joanne
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is a good description of singletons in Effective Java (=EJ) by Joshua Bloch. You can probably find a PDF of the old edition somewhere; I have one which claimed to be a legal download.
The correct way to create a singleton nowadays is probably this:-That would not be in the old edition of EJ because it was written before enums. Since enums elements are full‑blown objects, you can add fields and methods to them. Look in the Java Tutorials.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sheriff ;-),for your suggestion.Ranch is a great place to be at.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also read Singletons Are Evil. It's a pattern that has fallen into disfavor and is seldom the best solution to the problem.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote: . . . Singletons Are Evil. . . .
I can never understand that link.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that's it's not a very clearly written article. The debate style of writing is rather confusing. I got more out of the msdn article referenced at the top of this story. I should have linked to that one in the first place.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i found it difficult to understand too.Can anyone suggest a similar article that is easy to understand.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jro jhit wrote:Can anyone suggest a similar article that is easy to understand.

Do you mean other than...
J. Kevin Robbins wrote:the msdn article referenced at the top
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this one.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!