| Author |
Why constuctor can't be marked as final?
|
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
|
Thanks in Advance.
|
Tell the difficulties that i am difficult.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
|
final just means it can't be overridden. A constructor can't be overridden, so what's the point of labelling it final? With most of the examples where a particular modifier isn't allowed in a particular place in Java, the explanation is simply that it wouldn't make much sense there.
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Matthew Brown wrote:final just means ..
Thanks Matthew
I got another link also Constructor final
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
but i make a constructor as final by not using final keyword instead I use private ;)
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Seetharaman Venkatasamy wrote:but i make a constructor as final by not using final keyword instead I use private ;)
If final is useless here then private also should be, what is use of private here as we can't inherit constructor.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
saloni jhanwar wrote: what is use of private here as we can't inherit constructor.
you cant subclass a class[or you cant instantiate a class using that constructor]
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Seetharaman Venkatasamy wrote:
saloni jhanwar wrote: what is use of private here as we can't inherit constructor.
you cant subclass a class[or you cant instantiate a class using that constructor]
I want to ask that as we can't inherit constructor then why to use access modifiers with it ,like private etc because they will be useless for constructor.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
Constructor is a *special* kind of method. and how do you create an instance[by using new operator] of a class with out seeing the constructor ?
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Seetharaman Venkatasamy wrote:Constructor is a *special* kind of method. and how do you create an instance[by using new operator] of a class with out seeing the constructor ?
Thanks but my question was something different, yes i know constructor is needed for class, but is there any sense to use any access modifiers with it ?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
|
Making a constructor private will (if it's the only constructor) prevent you from instantiating the class from outside the class. Which means it can only be instantiated from inside - e.g. in a static method. See, for example, the Singleton pattern.
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
saloni jhanwar wrote:but is there any sense to use any access modifiers with it ?
Well, the answer lies in Seetharaman Venkatasamy's post (previous to your last post). What if I want to 'stop' someone(external to my class) from creating objects? What if I want methods of 'my class' only should be able to create the objects?
I hope you get the point.
Edit : I was typing this when Matthew Brown replied, so its almost duplicate
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
A common example I learn from the industry:
This is how people usually do to create a singleton assuming no two threads accessing getA() method concurrently.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
That wouldn't work - you're trying to access A's private constructor from B. You'd have to move the static method to A (and fix the other problems with it )
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
Sorry,
I type it wrong, the revised code should be:
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Now there's just the "other things wrong with it" . The most important being, that will actually create a new A every time you call getA().
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
The code should be this:
Helen Ma wrote:
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
Nops... it still have a problem
Consider below scenario:
1) There are two threads which are calling getA method.
2) Thread 1 reaches line 10. a is null, so it reaches at line 11. Line 11 has not yet executed.
3) Thread 2 reaches line 10. a is null, so it reaches at line 11. Line 11 has not yet executed.
4) Thread 1 creates an object, assigns to 'a' and returns the object.
5) Thread 2 creates another object, assigns to 'a' and returns the object.
So, there are two possibilities to avoid this:
Either
Or
Personally, I prefer the first one for below reasons:
1) Instance is initialized during class loading itself.
2) There's no 'if' condition overhead during getInstance method.
3) We don't have to explicitly make it thread-safe.
I hope this helps.
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
Hi, thanks for your thread safe example.
By the way, this quoted code:
is equivalent to
So, if "this" Singleton instance is not created yet, will I get a null pointer exception?
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
No. The equivalent code which you have posted will give compile time error (for the exact same reason - you cannot use 'this' inside a static context/method).
The code which I posted contained a static synchronized method which is equivalent to:
Besides, my original code had a problem - getter method was void!
I've edited that post.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
You want the easiest way to make a singleton in Java? Guaranteed thread safe as well?
(Using singletons is often dubious, but I'm not going to get into that now)
|
 |
 |
|
|
subject: Why constuctor can't be marked as final?
|
|
|