Question

everybody. I have a Java Eclipse project. It contains many packages, and the class names can be the same with the package, just like:

org.a
  a.java <--------- this class is org.a.a
  b.java
org.b
  a.java
org.a.a <---------- this package is also org.a.a
  a.java
  b.java

When compiling the project, Eclipse reports error: The type "xxx" collides with a package. So is there a Java IDE that supports the same package and class names? Or is there a way I can compile it just in Eclipse?

Was it helpful?

Solution

The short answer is no, if you have classes in different packages with the same name you will need to use their absolute names (package.name) to reference them in your code, this is the point of having package name spaces.

So in your code you would need to use org.a.a and org.a.a.a explicitly, rather than using the import statement...

For example...

org.a.a a1 = new org.a.a();
org.a.a.a a2 = new org.a.a.a();

OTHER TIPS

This is not a Eclipse issue, it is a Java feature.

The Java Language Specification makes the following restrictions

The members of a package are its subpackages and all the top level class types (§7.6, §8 (Classes)) and top level interface types (§9 (Interfaces)) declared in all the compilation units (§7.3) of the package.

and

A package may not contain two members of the same name, or a compile-time error results.

In your example, org.a is a package. You want it to have two members: the type org.a.a and the package org.a.a. The Java Language does not allow this.


Java conventions state that your package names should start with a lowecase letter while your type names should start with an uppercase letter. If you follow these conventions, such problems will never arise.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top