aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifiers 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 "Access Modifiers" Watch "Access Modifiers" New topic
Author

Access Modifiers

Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
I am getting a little confused with class and constructor modifier combinations.
If the class has default (friendly) access and the constructor has public access. What is the point of having a constructor with less restrictive access than the class?
If the class has public access and the constructor is private. Would this be used for accessing a utility class full of static methods? (e.g. java.lang.Math) What other reasons would there be?
Obviously, there are other combinations. What are the rules/reasons for choosing certain combinations.
Thanks
[This message has been edited by Jim Hall (edited December 09, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jim,
JLS 6.6.4 and ff
If the class has default (friendly) access and the constructor has public access. What is the point of having a constructor with less restrictive access than the class?
Good question and the answer is "there is no point" since your class is not accessible in other packages and thus the constructor can only be called from within the package in which the class is.
If the class has public access and the constructor is private. Would this be used for accessing a utility class full of static methods? (e.g. java.lang.Math) What other reasons would there be?
This is used in general when you want your class to be in control of the creation of its instances. In the case of the java.lang.Math class, there is no instance methods so there is no need for an instance, that's why they made the constructor private so that nobody can get an instance of it.
Ragarding the possible combinations, I suggest as an exercise that you draw a table and try to figure out what every combination means. This is a good exercise.
class\ctorprivatedefaultprotectedpublic
private??< /td>??
default????
protected????
public????

I'll try to fill in that table too but I don't have time right now...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[This message has been edited by Valentin Crettaz (edited December 09, 2001).]
[This message has been edited by Valentin Crettaz (edited December 09, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
class/constructorprivatedefaultprotectedpublic
privateN/AN/AN/AN/A
defaultAB CD
protectedN/AN/AN/AN/A
publicEFGH

A. Classes used within a package and are created by methods which control the creation of class objects. Or there is no need to create an instance of this class. The class' members are all static.
B. Classes used within a package. Any class within the package can use the class and create instances.
C. Constructors with less restrictive access than class is pointless. Actually same as B.
D. Same as C.
E. Same as A, but class access is public.
F. The class can be used by any other class, but you must be in the same package to create an instance.
G. The class can be used by any other class, but if you need to create an instance, you must subclass it.
H. These classes can be used by any other class and instances created without any restrictions.
Situation B & H seem pretty straightforward.
Situation C & D seem redundant.
The ones that cause some confusion are G, F, E, A.
My questions are not How? They are Why? Why make a particular class have more restrictive access to its constructors? It seems that most classes that I use in the Java 2 API are public.
Which brings me to a question a fellow student asked me about Java which I didn't understand. If you create a package of classes with some of the classes having package access. If I understand correctly no one should be able to access these classes from outside the package. But what if someone just adds the package name to the top of a class they create. Will they be able to access those classes? How does this work?
[This message has been edited by Jim Hall (edited December 09, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good job Jim,
I have a remark concerning A, though, which is:
all members are not necessarly static. Such classes may have instance member methods (or variables) but the instance are created by a static method (getInstance() for instance) which controls how many instances are created. Sometimes youi may want only one instance of your class to be created. The easiest way to do that is with a private constructor and a static method which will call the constructor if no instance has been created yet or return the only instance if it has already been instantiated.
Concerning the other cases, that's right some of them don;t make any sense...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers