search

Home  >  Q&A  >  body text

java - 类名和导入的类名冲突如何让编译器警告

是由这个问题https://segmentfault.com/q/1010000005725685引出的一个问题。

// Calendar.java
import java.text.DateFormatSymbols;
import java.util.*;

public class Calendar {
  public static void main(String[] args) {
    GregorianCalendar d = new GregorianCalendar();
  }
}

javac -Xlint Calendar.java编译时并没有报warning,是否有什么选项让编译器报warning。

黄舟黄舟2767 days ago484

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 09:16:53

    There will be no warning, you are overthinking it, because of your Calendar的命名空间(就是package)肯定不是java.util, right?

    So, the compiler must identify conflicts based on "namespace + class name", so unless you do it package也写成java.util. Otherwise, I think it would be a bit overwhelming to have the compiler report an error/warning!

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:16:53

    I just learned Java this morning, and you guys confused me. I feel like I didn't expect it to be so complicated, I simply wrote the class name and file name wrong. It is normal that no warning is reported in this status. After all, the namespace of this class is different from the namespace of java calendar.

    reply
    0
  • Cancelreply