• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access modifiers question  RSS feed

 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question
public class A
{
A()
{
}
}
1) The class A can be referenced outside the package in which it is defined.
2) The class A cannot be instantiated outside the package in which it is defined.
3) The class A cannot be extended outside the package in which it is defined.
4) The class A can be referenced, instantiated or extended anywhere.
5) The above code will cause a compiler error. The constructors of public class have to be public.
Answer is 1,2,3. Shouldn't it be 4, since the class is public. Constructors have the same access modifier as the class.
Thanks
Sharda
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors have the same access modifier as the class.
Wrong.
Please read JLS 8.8.3 Constructor Modifiers for detailed information about constructor modifiers.
It is throughout possible to have a private constructor in a public class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!