[英]What to do if 2 classes have the same package statement
I'm running into a problem where 2 classes from 2 different packages (owned by the same group) have the same name and package statement我遇到了一个问题,其中来自 2 个不同包(属于同一组)的 2 个类具有相同的名称和 package 语句
Package 1 Package 1
package com.placeholder.constants.Constants
public class Constants{}
Package 2 Package 2
package com.placeholder.constants.Constants
public class Constants{}
If I'm working on Package 1 but want to reference this Constants class in Package 2, is this even possible?如果我正在研究 Package 1 但想在 Package 2 中引用此常量 class ,这是否可能?
where 2 classes from 2 different package
其中来自 2 个不同的 package 的 2 个类
But they are not two different packages.但它们不是两个不同的包。 You've used the same package name for both.
您为两者使用了相同的 package 名称。
want to reference this Constants class in Package 2
想在 Package 2 中引用这个常量 class
Use either:使用任一:
You cannot define two classes with the same name in the same package, for obvious reasons.由于显而易见的原因,您不能在同一个 package 中定义两个具有相同名称的类。
The purpose of packages in Java is to create a namespace . Java 中的包的目的是创建一个命名空间。 Your duplicate class name violates that namespace.
您重复的 class 名称违反了该命名空间。
package com.placeholder.constants.Constants
That is not a proper package name.这不是正确的 package 名称。 You've mixed the class name into the package name.
您已将 class 名称混合到 package 名称中。 The package should be
package com.placeholder.constants
. package 应该是
package com.placeholder.constants
。
This cross-naming problem of yours raises the possibility that you have a less than optimal class design.您的这个交叉命名问题增加了您的 class 设计不是最优的可能性。 You might want to pause a moment to look at the bigger picture.
您可能想暂停片刻,看看更大的图景。
And if you have that many constants to name, I wonder if some of those should really be enum
types.如果你有那么多常量要命名,我想知道其中一些是否真的应该是
enum
类型。 See tutorial by Oracle .请参阅Oracle 的教程。 Tip: In Java 16 and later, enums can be defined locally, within an a method — a new alternative to being defined as a nested class or defined as a separate class.
提示:在 Java 16 及更高版本中,枚举可以在本地定义,在一个方法内 - 定义为嵌套 class 或定义为单独的 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6DZ 的新替代方法。
You are facing a name conflict.您正面临名称冲突。 If possible, try to enter the full name of the package.class_location
如果可能,尝试输入package.class_location的全名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.