• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Question - Philip Heller's book

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book states :
" .... This "freebie" constructor is called the default constructor. It has public access if the class is public; otherwise its access mode is default "
few pages later it says: (chapter summary )
" ... This is the default constructor; it takes no arguments and is of public accessibility. "
My question is: which one is correct ? Is the default constructor always public like the second sentence makes me believe ?
Thanks
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a constructor is given no access modifier, then its access modifier is implicitly that of whatever the class has, ie coderanch, protected or private. If the class has no access modifier, ie has default package access, then so does the constructor.
The only access modifiers which a constructor can optionally be declared with are coderanch, protected or private.
Note that you can mix them, eg a public class can have a private constructor, though you won't be able to instantiate the class from outside.
[ May 04, 2003: Message edited by: Roger Chung-Wee ]
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do not specify any constructors then Java automatically adds an invisible default constructor, from the moment you specify your own constructors with parameters Java will not add a default constructor.
The default constructor gets the same access modifier as the class access modifier.
If the class has public visibility, the default constructor has public visibility.
If the class has protected visibility, the default constructor has protected visibility.
If the class has private visibility, the default constructor has private visibility.
If the class has default visibility, the default constructor has default visibility.
 
Thomas De Vos
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules specified in my reply above are intuitive.
This doesn't mean that when the constructor is accesible when the class is accessible !!!
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger and Thomas ,
I am little bit confused , with your responses.
I been thinking a class can only have default or public access.This is what I think I read and understood from Kathy/Bert book.
I know a constructor can have coderanch,protected,private and default access.
Could someone please clarify.
Archana
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
package level class will have default and coderanch, but enclosed classes can have coderanch, protected, private, abstract, static, final.
like
class outer{
class inner{ // here a class can be declared with any of the access specifiers
}
}

badri
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A top level class can only have the public access modifer. A nested member class can have one of the acccess modifiers (coderanch, protected or private) and can be static (does not apply to inner classes which by definition are non-static). Of course, they don't have to have any modifiers, in which case they will have default package access.
All can have the other modifiers (abstract, final and strictfp). Note that using both abstract and final makes no sense, so it is a compiler error. Furthermore, where more than one modifier is used, the recommended order of declaration is coderanch, protected, private, abstract, static, final, strictfp.
Here are some examples.

[ May 05, 2003: Message edited by: Roger Chung-Wee ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
A top level class can only have the public access modifer.

A top level class can be public or default.
In fact, since you can have as many classes as you like in a single file (not nested) and since the file must be named after the one and only public class in the file, this rule is required.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic