• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private Constructors  RSS feed

 
Achint Verma
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class. [From Kathy & Bates]

The first part of the above statement is clear to me. I am not very clear about what does the second half(...so if the private....) of the statement mean to say. Could someone elaborate more upon the second part of the statement ? Examples are welcome.



This is what I understand of the second part: To use an instance (i.e., n) of the private constructor class A, I must provide a static method or variable to access n. So accessing 'n' from non static method checkB shouldn't work. But it does work.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted will cause a StackOverflowError when you try to use class A. Why? Because of this:

What happens if you instantiate class A? It will initialize the member variable n. To initialize the member variable n, it will create a new instance of A. But that new instance will itself have a member variable n, which is going to be initialized by creating a new instance of A. That one again will have a member variable n, which is going to be initialized by creating a new instance of A. And that new instance, again, is going to have a member variable n, which is going to be initialized... etcetera, until the stack overflows.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are thinking of is a factory method. There is a very simple example above. You will find more discussion of a similar question here.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the sort of thing they're talking about:
Now, in that particular case you haven't gained anything. But you can change getInstance() to, for example, always return the same instance, or always return one of a fixed number of instances.
 
Achint Verma
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I get my StackOverFlow blunder. But now for your code


if I make getInstance a non-static method ...the code still seems to work ? The rule says it has to be a static variable. Eclipse doesn't show up any error if i do away with static. Pleaze explain. I am sorry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achint Verma wrote:if I make getInstance a non-static method ...the code still seems to work ? The rule says it has to be a static variable. Eclipse doesn't show up any error if i do away with static. Pleaze explain. I am sorry

It will compile if you make it non-static. But have you tried to use it like that?

To call a non-static method, you need an instance of the class to call it on. But the only way for another class to get an instance is to call that method. Which means it needs an instance before it can get an instance. See the problem?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:To call a non-static method, you need an instance of the class to call it on. But the only way for another class to get an instance is to call that method. Which means it needs an instance before it can get an instance. See the problem?

@Achint: Just to add to what Matthew said, the pattern you describe is called an instance factory; and it does have its uses, particularly in class hierarchies. For example:The above example is a bit contrived, but instance factories can certainly be useful for classes that have many internal values, particularly when they can be shared between instances.

HIH

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achint Verma wrote:Okay I get my StackOverFlow blunder. But now for your code


if I make getInstance a non-static method ...the code still seems to work ? The rule says it has to be a static variable. Eclipse doesn't show up any error if i do away with static. Pleaze explain. I am sorry



As Matthew and Winston already stated.... K&B was referring to a pattern and not a rule. So, there is no reason that your changes shouldn't compile -- by making it a non-static method, you are just creating something different from the pattern; something that may compile but doesn't work.

And BTW, this pattern is not the only option either. It is just the best-practice pattern developed over the years. If is, of course, possible to get an instance using a different instance altogether (hence, no static method or static variable) ....



which you can use to get an instance of A like this...




Now, admittedly, this is just silly.

Henry
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that what's tripping you up is the "static" part. The method or variable that provides access to an instance of the class with a private constructor HAS to be static. Think about it: if a class, say Foo, has a private constructor that can't be invoked by any other class, then new Foo() won't work outside of Foo itself. Since you can't use new Foo() outside of Foo, the only other ways to get a Foo instance is to have the Foo class expose either a static variable of type Foo or a static method that returns a Foo instance. Non-static methods and non-static properties can't be used until you actually have an instance of Foo.
 
Pablo Abbate
Ranch Hand
Posts: 30
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you do something like that when you want to use the singleton pattern. Please check Singleton Wiki
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Abbate wrote:Usually you do something like that when you want to use the singleton pattern. Please check Singleton Wiki

Actually, that's not quite true. Static factories are used all over the place (eg, String, and all the standard wrapper classes), because they have many advantages over public constructors, particularly in conjunction with immutable objects.

And haven't you heard? Singletons are evil.

Winston
 
Pablo Abbate
Ranch Hand
Posts: 30
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pablo Abbate wrote:Usually you do something like that when you want to use the singleton pattern. Please check Singleton Wiki

Actually, that's not quite true. Static factories are used all over the place (eg, String, and all the standard wrapper classes), because they have many advantages over public constructors, particularly in conjunction with immutable objects.

And haven't you heard? Singletons are evil.

Winston


The idea was to explain a possible implementation. The main goal is to control the objects creation, a singleton is just an example.

Jaja I read the post ... Interesting ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!