• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package name and class name

 
lyko saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 6: Objects and Classes (Sun Website) By Simon Roberts, Philip Heller, and Michael Ernest
The authors mentioned that:
�It is illegal for a package and a class to have the same name, so there can be no ambiguity.�

However, when I tried to compile the following code, it was successful.
Why is it so? I mean, how can I prove it?

package P1;
class P1 {}

This is saved as P1.java in a directory P1 (which is again in a directory specified in my classpath)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lyko,
Welcome to JavaRanch! I think that means that you can't have a package and class with the same name at the same level.

For example:
1) package jeanne.Test;
2) package jeanne;
class Test() { ... }

This would be illegal because jeanne.Test would be ambiguous.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the fully qualified name of the class you posted is P1.P1, while the name of the package is simply P1. So, P1.P1 != P1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic