• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why constuctor can't be marked as final?

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks in Advance.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:final just means ..


Thanks Matthew

I got another link also Constructor final
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i make a constructor as final by not using final keyword instead I use private ;)
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
I type it wrong, the revised code should be:

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code should be this:

Helen Ma wrote:
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic