File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Help understand this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help understand this" Watch "Help understand this" New topic

Help understand this

Ranch Hand

Joined: Mar 06, 2001
Posts: 123
Hi all,
I'm having some difficulty in understanding
this concept ... I have a class that has package level access. This class has public constructor.
class Class A{
public Class A(String name) {}
So my question is (1) Does it make sense to have a *public* constructor? Since the class is visible only within the package
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Yes, it does. Access modifiers on constructors, methods, and data fields tell whether or not it can be accessed from outside the class. So if the constructor were private, then it couldn't be accessed by other classes, even if they are in the same package.

Java API Documentation
The Java Tutorial
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
but what Prasanth is wondering is whether the package access modifier for the class then implies that there will be no access for the method any wider than package, so why call it public?
is that right Prasanth?

giddee up
Ranch Hand

Joined: Mar 06, 2001
Posts: 123
Thats right Jasper. I have seen some code snippets in some Java books that have samples I illustrated earlier. I felt that they could have left access level for the constructor at package level (or private) since the class is at package level.
class ClassA{
ClassA(String name) {}

So I'm failing to understand the reasoning behind making the constructor (or any methods) public while leaving the class at package level
[ January 24, 2003: Message edited by: Prashanth Joisha ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
If the class is only accessible within the package, then I cannot think of a good reason to still label methods or constructors as being more accessible when they clearly are not. To do so might arguably be a bad idea as it would possibly cause mild, momentary confusion among otherwise distracted users of the class.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Help understand this
It's not a secret anymore!