• Post Reply Bookmark Topic Watch Topic
  • New Topic

Brain Barbell  RSS feed

 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the book "Head First Java":

We saw that a private constructor means that a class can't be instantiated from code running outside of the class. In other words, only code from withing the class can make a new instance of a class with a private constructor. (There's a kind of a chicken and egg problem here.)

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 the one, single instance?"


First i don't understand the chicken and egg analogy, i tried figuring out what they mean but couldn't get the idea. I've come so far as to understand that the constructor has to be public in order for anyone to be able to even create a single instance of it. However, that seemingly results in the ability to create several instances of that class.

I have a few theories of how to solve the problem all of which turn out to be way to complicated to explain in text and also causes intricacies of which i'm not able to understand. This makes me think they're all wrong. Hence i would like a few pointers on how i should think in order to solve the problem.


Regards

Daniel
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Roberts wrote:First i don't understand the chicken and egg analogy,

I think basically what they're saying is that you can't create an instance of such a class outside the class, but you have to have an instance of the class in order to run the constructor. However, that's not strictly true, because it could define a static factory method - so I can understand your confusion.

So beyond that: what is it you're having difficulty with? How to prevent a class creating more than one instance of itself - ie, a Singleton?

If so, the normal pattern is:but TBH, these days it's easier just to define an enum with a single value.

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find yourself a copy of Effective Java™ by Joshua Bloch. In my copy (2nd edition) page 5 shows how you can combine a static factory method with private constructors to provide access to instances. Page 17 shows how you can get one instance only.

There is a lot of controversy about one instance only, which is called a Singleton. There are variations whereby you have exactly n instances (e.g. two instances) but you still call that a variation of a Singleton. About ten years ago Singletons were considered a good thing but opinion has changed and they are now thought not good at all. Since the enum keyword was added to the language in 2004 (Java5), an enumerated type has become the best way to create a single instance.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also classes like utility classes (java.lang.Math) is a good example. They exist to provide methods rather than fields. So all their members can be static and there is no need ever to instantiate such classes. So they have private constructors which are never called from inside the class.
 
Deepankar Narang
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read Head First books and honestly it takes some time to get the humour they add. Looking at your question, it seems you have just started learning any OOP language. Considering this, I am going to keep it very simple here.

Humour -
There's a kind of a chicken and egg problem here.

Head First team is trying to tickle your brain to look into the alternatives or trying to make you aware that you have not asked few what if questions yet. As far as I remember, at this point they have introduced methods and public/private(/protected) keywords. You have also understood constructor is broadly a method with some special meaning,usage and restrictions. Now the Author(s) are trying to summarize and link different things you have learned. All they want here is to make you aware, that you have learned constructors are mandatory to instantiate a class, they should be public and they are also kind of methods. As they are methods, does public/private(/protected) keywords apply to them. If it does, what it means. If we make them private, you cant instantiate the class from outside. You can only interstate such a class within the class. But to do that you need an instance of a class which you can't get as constructor is private.

At this point they would introduce you to static keyword, starting with a static function. Read on the book and enjoy the ride. Most of the questions would be answered, if not come back to us and we would help you. (As if now its just a half baked cake which is chaotic to handle).

Head first actually achieved what they wanted. You really sat and thought about the alternatives, which is absolute brilliant! Never loose this. Cheers


 
Danny Roberts
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for both of your replies!

It seems the misinterpretation from my part was that i thought i was somehow supposed to be able to gain access to the class from the outside(eg from the main class) which was why i was having trouble understanding the problem. The book "Effective Java" does a good job of explaining what a singleton is but i'm still not understanding how only ONE instance of it can be created. Say on page 17 for example, if i were to write: public static final INSTANCE2 Elvis = new Elvis() below the first initialization of INSTANCE, did i not just create another instance of the class?

Edit: Deepankar, thank you for those encouraging words, from now on i'll be aware that the questions asked in order to get to the answer are far more important than the answer itself.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Roberts wrote:Say on page 17 for example, if i were to write: public static final INSTANCE2 Elvis = new Elvis() below the first initialization of INSTANCE, did i not just create another instance of the class?

Sure, but nobody else can do that; only YOU.

So presumably you've decided that you want your class to be a "doubleton"....

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepankar Narang wrote: . . . constructor is broadly a method with some special meaning,usage and restrictions. . . .
No, a constructor is not a method. It might look like a method, but that is where the similarity ends.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Roberts wrote:Thank you for both of your replies! . . .
You're welcome
public static final INSTANCE2 Elvis = new Elvis() below the first initialization of INSTANCE . . .
As Winston has already told you, yes. I said you can have a variation of the Singleton which has two instances.
 
Deepankar Narang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Deepankar Narang wrote: . . . constructor is broadly a method with some special meaning,usage and restrictions. . . .
No, a constructor is not a method. It might look like a method, but that is where the similarity ends.


I agree with you. They look same and that's where it ends. And that's all I intended to convey by using "broadly". I was trying to explain the context used in Head first Books
 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right so everything seems to makes sense now except one thing. Winston, you wrote that no one else except me can make that second singleton. How would "someone else" even try to create another instance of the class? Also, how would i go about to use that single instance of the class as "someone else"? This is exactly why i misinterpreted the problem form the start as i saw the "someone else" being a person trying to access the class from a main class(sort of like being on the outside), as the problem stated:
anyone who wants to use an instance of the class will always use that one,single instance.
I'm interpreting that anyone individual being a certain "someone else". I feel like i'm missing something important.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Roberts wrote:I'm interpreting that anyone individual being a certain "someone else". I feel like i'm missing something important.

I'm assuming you wrote this class, so you are the author. However, if the class is public, then anyone can USE it, so the question then becomes:
How do you want clients (them) to use it?

In the first case, you've given them one instance they can use, in the latter, two; but nothing will change for your clients until YOU make a change to your class and compile it.

Does that explain it? When you write a class, you're its "boss".

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no certain “someone else”. What Winston means is that the only person who can create that second Elvistic instance is whoever writes the Elvis class. If anybody uses the Elvis class, never mind who they are, they get whichever instance the method returns. In the version Bloch wrote, there is only one instance of Elvis, so whoever uses that class always gets the same instance. It doesn't matter who it is, every user whether certain or unspecified, gets the same instance. The only way to get two instances is to run two JVMs separately.
 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes complete sense. Thank you guys very much for spending your precious time helping me out. Much appreciated!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!