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 package name and class name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package name and class name" Watch "package name and class name" New topic

package name and class name

lyko saha

Joined: Aug 21, 2004
Posts: 5
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 in a directory P1 (which is again in a directory specified in my classpath)
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30138

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Don't get me started about those stupid light bulbs.
subject: package name and class name
Similar Threads
how to make two classes in a packahe where one class extends the other
Accessing a class of default package
package & classpath I am lost !
Q of inner class in different package
Reg. Packages