Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit Constructor

 
Akash Roy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the default constructor has public access
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the default constructor has the same access modifier the enclosing class
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony is correct, of course. (I was assuming a top level class.)
[ February 04, 2005: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic