aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question - Philip Heller's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question - Philip Heller Watch "Question - Philip Heller New topic
Author

Question - Philip Heller's book

Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
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


Giselle Dazzi<br />SCJP 1.4
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If a constructor is given no access modifier, then its access modifier is implicitly that of whatever the class has, ie public, 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 public, 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 ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
The rules specified in my reply above are intuitive.
This doesn't mean that when the constructor is accesible when the class is accessible !!!
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
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 public,protected,private and default access.
Could someone please clarify.
Archana
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
Hi,
package level class will have default and public, but enclosed classes can have public, protected, private, abstract, static, final.
like
class outer{
class inner{ // here a class can be declared with any of the access specifiers
}
}

badri


Thanks<br />Badri
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
A top level class can only have the public access modifer. A nested member class can have one of the acccess modifiers (public, 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 public, protected, private, abstract, static, final, strictfp.
Here are some examples.

[ May 05, 2003: Message edited by: Roger Chung-Wee ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
Consider Paul's rocket mass heater.
 
subject: Question - Philip Heller's book