I want to make a package redirect to another package. For example I have a package called “packageA” and another called “packageB”, and importing packageA would import packageB. I want to be able to do this without copying the package. Also, is there any way to run a method if packageA was imported instead of packageB?
Share
Let’s look at how the
importstatement works:First of all,
importonly imports classes or interfaces. It never imports a package. In particular, something likeimports all the classes and interfaces in the package named
java.util. This package is simply a container for the classes and interfaces, so it really doesn’t make any sense to talk about “importing a package”. A package by itself isn’t anything that compiles or runs.With that said, it doesn’t make any sense to talk about “redirecting” because what are you going to redirect to? If you want to import a class from “packageA”, just import it directly.
Similarly, importing a class from packageA gives you access to that’s classes methods. Since that class lives in packageA, it doesn’t make any sense trying to import it from packageB. Even if packageB has a class with the same name, they are two different classes. In fact, this is the primary reason for packages in Java: to avoid name clashes for classes written by two different programmers.
With all that said, what problem have you encountered that you are trying to solve with “redirecting imports”?