[英]In Java, if a class implements an interface, do I need to compile the interface?
My previous thinking was that I had to compile the interface along with the implementing class for the implementing class to understand static data belonging to the interface. 我以前的想法是,我必须将接口和实现类一起编译,以使实现类理解属于该接口的静态数据。
For instance if the interface has a data field, public static final int six = 6
, if I wanted to use six
in the compiled class and run a program using this class and uses six
, I would have to include the compiled interface class also. 例如,如果接口有一个数据字段, public static final int six = 6
,如果我想用six
使用这个类在编译的类并运行程序,并使用six
,我将不得不包括编译接口类也。
This does not seem like the case, how does the implementing class even know that it has this data field without the compiled interface? 情况似乎并非如此,实现类甚至如何知道它具有不带已编译接口的数据字段?
If I wanted to use
six
in the compiled class and run a program using this class and usessix
, I would have to include the compiled interface class also. 如果我想用six
使用这个类在编译的类并运行程序,并使用six
,我将不得不包括编译接口类也。
However , a static final
value will likely be inlined by the java compiler to the constant value (at compile time ). 但是 ,java编译器可能会将static final
值内联到常量值(在编译时 )。 You can examine the byte-code with something like 您可以使用以下方法检查字节码
javap -v MyClass.class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.