I'm trying to create two different classes with the same names but in two different packages in test/java branch for Maven project.
src/test/java/home/MyClass.java
src/test/java/body/MyClass.java
It's only for test/java branch for Maven project.
src/test/java
is a source folder for java files. If you have the same class with the same name in different packages then it's not good because a code duplication. But if you have different classes with the same name reside in different packages then it's ok. The package
directive in Java is used to avoid name collisions that happens if you use the same class name for different classes.
This question is also asked here:
Use the
package
name. This type of problem is precisely why Java uses the package naming convention that it does. It prevents these sorts of problems, whether it's two teams in the same company or two teams on opposite sides of the earth.
You can use both classes with the same name, but you need to specify FQCN (Fully Qualified Class Name) for each one in the code if you use them simultaneously. More about it Handle Classes With the Same Name in Java.