aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default constructor" Watch "Default constructor" New topic
Author

Default constructor

S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Can anyone give about the access modifier for the default compiler generated constructors.
If the class is a outer class what is the access modifier applied?
If the class is a inner class what is the access modifier applied?
If the class is top level nested class then what is the access modifier?
pramodh somashekara
Ranch Hand

Joined: Jan 30, 2006
Posts: 62
i think...the compiler generated constructors have same access as that of the class...correct me if i am wront
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
What if:

Now what could be the access modifier for the default construtors for A & B.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
According me constructor A has default access and constructor B has private
access. Please correct me if i am wrong.
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
A constructor having a private access can be useless since it cannot be accessed by any other class. Its another way to avoid creating an object of that class.

As a standard, we should not put any access modifier for construtors. Thus, for every constructors java automatically assigning default access modifier.


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
Atul that's not correct. Look at common impls of the Singleton pattern...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
A constructor having a private access can be useless since it cannot be accessed by any other class.

A counterexample:


[ July 17, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification.

8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

* If the class being declared is the primordial class Object, then the default constructor has an empty body.
* Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have an accessible constructor that takes no arguments.

A default constructor has no throws clause.

It follows that if the nullary constructor of the superclass has a throws clause, then a compile-time error will occur.
In an enum type (�8.9), the default constructor is implicitly private. Otherwise, if the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118
Class Modifiers are modifiers for Default constructors.


Thanks & Regards,
ChandraSekharMangipudi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default constructor