• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help understand this

 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Thanks,
PJ
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Thanks,
PJ
[ January 24, 2003: Message edited by: Prashanth Joisha ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic