Eclipse Java - 无效的包名 - 包名中的保留字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13269318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:21:49  来源:igfitidea点击:

Eclipse Java - invalid package name - Reserved words in package name

javaandroideclipsepackage

提问by Krishnabhadra

I am in the middle of an android project and was trying to create a new package in it.

我正处于一个 android 项目的中间,并试图在其中创建一个新包。

com.mycompany.myprojectname.new

Well, Eclipse is not letting me to create it and is showing this error:

好吧,Eclipse 不允许我创建它并显示此错误:

Invalid package name. 'new' is not a valid Java identifier

包名无效。'new' 不是有效的 Java 标识符

I never knew package name has reserved words, which we cannot use. My questions are;

我从来不知道包名有保留字,我们不能使用。我的问题是;

  1. Is this an Eclipse thing? or a Java thing? I tried a pure Java project (not Android), just to check, but there also I got the same error.
  2. What are other reserved words that is not allowed?
  3. Is there any documentation about this?
  1. 这是 Eclipse 的事情吗?还是Java的东西?我尝试了一个纯 Java 项目(不是 Android),只是为了检查,但我也遇到了同样的错误。
  2. 还有哪些不允许使用的保留字?
  3. 有没有关于这个的文件?

回答by Joachim Sauer

Yes, this is a general Java thing.

是的,这是一个通用的 Java 东西。

The list of reserved words can be found here. They are:

保留字列表可以在这里找到。他们是:

abstract  continue    for         new         switch
assert    default     goto        package     synchronized
boolean   do          if          private     this
break     double      implements  protected   throw
byte      else        import      public      throws
case      enum        instanceof  return      transient
catch     extends     int         short       try
char      final       interface   static      void
class     finally     long        strictfp    volatile
const     float       native      super       while
abstract  continue    for         new         switch
assert    default     goto        package     synchronized
boolean   do          if          private     this
break     double      implements  protected   throw
byte      else        import      public      throws
case      enum        instanceof  return      transient
catch     extends     int         short       try
char      final       interface   static      void
class     finally     long        strictfp    volatile
const     float       native      super       while

Documentation on the fact that reserved words can not be used in package names if found in the package naming tutorial, among other places.

如果在包命名教程中找到保留字,则包名称中不能使用保留字这一事实的文档,以及其他地方。

The authoritative source is (as always) the Java Language Specification, specifically:

权威来源(一如既往)是 Java Language Specification,特别是:

回答by Marcin Orlowski

See docs here:

请参阅此处的文档:

http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int".

在某些情况下,互联网域名可能不是有效的包名称。如果域名包含连字符或其他特殊字符,如果包名称以数字或其他字符作为 Java 名称的开头是非法的,或者 包名称包含保留的 Java 关键字,则可能会发生这种情况,例如“int”。

回答by MysticMagic?

newis a java keyword. Use some other word instead of it.

new是一个java关键字。用其他词代替它。