File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit 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 "Implicit Constructor" Watch "Implicit Constructor" New topic
Author

Implicit Constructor

Akash Roy
Greenhorn

Joined: Jan 08, 2005
Posts: 16
Hi,
Suppose i define a class public class Test{} in a package abc. But do not define any constructor. So in this case an implicit default constructor Test() will be provided by Java. But then how can the class be instantiated from outside the package abc. Since the constuctor Test() will have a default scope. Thanking in Advance.

Regards,
Vallabh
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A default constructor provided by the compiler has public access if the class itself is public, or default access otherwise. So in this case, the default constructor will be public.

However, if Test is in a package abc, then classes outside of that package will need to import abc in order for the Test class to be visible.

EDIT: Note clarification below.
[ February 04, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
the default constructor has public access


sandy
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
the default constructor has the same access modifier the enclosing class
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

A default constructor provided by the compiler has public access if the class itself is public, or default access otherwise.


Not quite true.
JLS 2e 8.8.7 states:

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.


Tony Morris
Java Q&A (FAQ, Trivia)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Tony is correct, of course. (I was assuming a top level class.)
[ February 04, 2005: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit Constructor