File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default or public ? 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 "default or public ?" Watch "default or public ?" New topic

default or public ?

niku shah
Ranch Hand

Joined: Aug 28, 2006
Posts: 43

In K&B book some mock exam questions ,

class declarations does not contain public specifier ,
so isn't it a mistake ?

what i mean is it may be assumed that class specifier is public ,
as we have to save file name with public class name.

and also options doesn't contain abt public class specifier ,
as there should be one public class in a file name to be executed.


Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
There can be max one public class per java file.
If there are two or more classes in a java file then name of java file and
public class should be same.
if there are two are more non public classes in java file
then there is no rule applied on naming on java file compare to class file.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Jay Suttiruttana
Ranch Hand

Joined: Aug 17, 2006
Posts: 36
class that does not have a declare modifier automatically gets "default" modifier. So your class can either have public or default access modifier.
niku shah
Ranch Hand

Joined: Aug 28, 2006
Posts: 43
Thanks All
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


Its not mondator, atleast one class should be public in single source file, but there can be only one public class allowed and source file name must match it.

If public is not specified then default will be assumed.

Remember , there are only 2 modifiers are present for class, public & default.
I agree. Here's the link:
subject: default or public ?
It's not a secret anymore!