• 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

JLS - Access Modifier

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello All...
This is from JLS 8.8.8 Preventing Instantiation of a Class
"If the class is declared public, then the default constructor is
implicitly given the access modifier public"
But in the same page, (the Example appearing at the end of the page)
Example:-
package just;
public class PackageOnly
{
PackageOnly() { }
String[] justDesserts = { "cheesecake", "ice cream" };
}
" The class PackageOnly can be instantiated only within the package
just, in which it is declared."

Can someone explain me why the class PackageOnly can be instantiated
only within the package just-- whereas it is mentioned that the
constructor will have access modifier depending upon the Access Modifier
of the class.
Thanks.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You read it wrong, it says, "...default constructor is
implicitly given the access modifier public"
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Try out the world's only WebCompiler!
www.jdiscuss.com
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi anil,
does the statement on line 4
PackageOnly(){} is not considered as default const just bcoz it is explicitly given???
please explain
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preeti,
PackageOnly(){} is still the default constructor as it matches signature for the default ctor; however, because it's explicitly declared the compiler does not assume the access modifier matches the class access modifier.
If no default ctor is declared the compiler supplies one. Rather than guessing at what the access modifier should be, it uses the class modifier. As in this case the default ctor is explicitly declared, the compiler doesn't supply another and assumes that you, the programmer, want package access.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic