【发布时间】:2015-02-13 11:47:56
【问题描述】:
假设,我在测试包中有一个测试类:
package test;
public class Test {
private static int clarying=20;
public static void main(String[] args) {
clarying.Product.display(clarying); // this line is giving error
// The primitive type int of
// clarying does not have a field Product
}
}
假设,我在 clarying 包中有另一个类 Product:
package clarying;
public class Product {
private static int test;
public static void display(int data) {
test = data;
System.out.println(test);
}
}
我已经编译了 Product 类,现在我正在尝试编译 Test 类,但它引发了编译器错误:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
The primitive type int of clarying does not have a field Product
at test.Test.main(Test.java:5)
问题已解决:
clarying.Product.display(clarying);
因为变量名在Test类中声明,所以和包名一样声明。因此,当我编写 clarying.Product 时,它正在 clarying 类变量中搜索字段 Product。
我只是想澄清一下:是否有任何规则反对定义与包同名的变量?
【问题讨论】:
标签: java variable-names package-name