• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access modifiers question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic