Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Demonstrating package names collision in java

Tags:

java

package

I got this question as an assignment:

Packages/Naming

We have created lot of packages and defined classes and interfaces in them. We have also discussed the point we have to remember while naming them. In this assignment we will see how important naming is.

Please change the package names in your previous assignment such that two packages have same name and analyze the result/errors that will be thrown.

My Doubt:

I am unable to think of a way(scenario) to demonstrate what has been asked. Since Java imports are absolute, the situation described in question seem impossible to produce (IMO).

Please help me to demonstrate this thing.

Thanks in advance :)

like image 597
ivorykoder Avatar asked Sep 25 '26 02:09

ivorykoder


1 Answers

Yes, if you change the package name you'll need to change the imports as well. If you happen to use Eclipse, there's an easy way to do this. In the Package Explorer view, right click on the package and do Refactor->Rename. It will rename the package and update all relevant imports in other classes.

Here's how it could work:

JavaProjectA
   src
     com.some.package
         ClassA
JavaProjectB
   src
     com.another.package
         ClassA

Fine so far. Each class is unique because of its package structure. However, its possible to create this:

JavaProjectA
   src
     com.some.package
         ClassA
JavaProjectB
   src
     com.some.package
         ClassA

Only one of those ClassA classes will get exported in a jar and used at runtime.

like image 89
Chris Knight Avatar answered Sep 26 '26 15:09

Chris Knight