• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor questions:Can constructor be static, synchronized, final,

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain of the constructor could be synchronized, static, final or private. Also please provide valid explanation.
Also can contructors have inheritance?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hemal,
I will let the JLS Specification speak for me here (Section 8.8.3).


a constructor cannot be abstract, static, final, native, strictfp, or synchronized. A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java virtual machine to verify that superclass constructors are always properly invoked during object creation.


I could not have said it better myself!
Regards,
Manfred.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hemal you can have private constructors.
Singleton class is a good example.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no other modifiers other than the access modifier are allowed for a contructor.
(access modifiers are public, protected, default and private)

Originally posted by Hemal Mehta:
Can anyone explain of the constructor could be synchronized, static, final or private. Also please provide valid explanation.
Also can contructors have inheritance?

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hemal Mehta:
Can anyone explain of the constructor could be synchronized, static, final or private. Also please provide valid explanation.
Also can contructors have inheritance?


I really like Manfred's response but, in short, you can only use the accessibility modifiers on a constructor (i.e. public, protected, private). You can leave off the access modifier and have a "default" accessible constructor but, since constructors aren't inherited, this is the same as having a protected constructor.
Public: Any class/object can create an instance of this class.
Protected/Default: Only classes/objects within this class' package can create an instance of this class.
Private: Only this class can create an instance of this class.
I hope that helps,
Corey
[ February 05, 2002: Message edited by: Corey McGlone ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hemal you can have private constructors.
Here is an example:
class DiffConstructors
{
private DiffConstructors(int i) {
System.out.println(" In the private constructor");
}

public DiffConstructors() {
System.out.println("In the public constructor");
}

}
public class TestDiffConst {
public static void main(String[] args) {
DiffConstructors d1 = new DiffConstructors();
// Not Possible to do this because it is a private constructor
//DiffConstructors d2 = new DiffConstructors(5);
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,

Protected/Default: Only classes/objects within this class' package can create an instance of this class.


So in your opinion protected and "default" accessibility of constructors is the same?
Well, I don't agree. What if your class has a "default" accessible constructor and one of its subclass is in a different package?
it doesn't work, that's right.
Code like the one below does not compile

The error is

HIH
 
reply
    Bookmark Topic Watch Topic
  • New Topic