Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

class access  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the following code and choose the correct options
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.
Answer given:3.
I think the answer should be 2 and 3.
how will the class be instantiated outside the package if it cannot be extended outside the package?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must've deleted my message.
You're correct, class A cannot be instantiated or extended outside of its package because the constructor is marked default. However, if you had public static fields defined in A, you'd be able to use them outside of the package.
 
sonali rao
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.
I have something to clarify. you said public static fields are accessible. Is it because the class access modifier is public? did i get this right?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sonali rao:
Thanks for the quick reply.
I have something to clarify. you said public static fields are accessible. Is it because the class access modifier is public? did i get this right?


Yes, if the class is declared as public and properly imported, you can use its static public fields and methods outside of the package. Even if its constructor is private (Math class is a good example of that).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!