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.
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
posted
0
the default constructor has public access
sandy
shandilya popuru
Ranch Hand
Joined: Dec 21, 2004
Posts: 95
posted
0
the default constructor has the same access modifier the enclosing class
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
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.