在编译时未评估的接口中定义的最终静态字符串 - Android
我有两个类和一个接口(例如 DatabaseModel
、LocalStore
和 InternalModelInterface)。它们的定义如下;
public class DatabaseModel {
// ...
public static final String KEY_PARAM1 = "param1";
}
public class LocalStore implements InternalModelInterface {
// ...
public void function () {
String temp = InternalModelInterface.COLUMN_PARAM1;
}
}
public interface InternalModelInterface {
public static final String COLUMN_PARAM1 = DatabaseModel.KEY_PARAM1;
// ...
}
我遇到的问题是,在运行时,当我调用 localStore.function()
时,temp 被分配为 null,因为 InternalModelInterface.COLUMN_PARAM1
为 null。这有道理吗? InternalModelInterface.COLUMN_PARAM1
不应该在编译时评估并内联吗?
这是针对 Android 应用程序的。提前致谢。
我将进一步解释以澄清任何混乱。
DatabaseModel
类的对象在解析 JSON 响应时实例化。 DatabaseModel
类中定义的常量表示要在 JSON 响应中查找的键。
InternalModelInterface
定义设备上本地(缓存)数据库中使用的列名称。由于多种原因(包括它们的键是 SQLite 中的非法列名),我不会重复使用这些键作为列名。
我使用接口而不仅仅是普通类的原因是该接口还指定了需要由第三个类 LocalStore 实现的必需方法。
I have two classes and an interface (for example DatabaseModel
, LocalStore
, and InternalModelInterface). They're defined as follows;
public class DatabaseModel {
// ...
public static final String KEY_PARAM1 = "param1";
}
public class LocalStore implements InternalModelInterface {
// ...
public void function () {
String temp = InternalModelInterface.COLUMN_PARAM1;
}
}
public interface InternalModelInterface {
public static final String COLUMN_PARAM1 = DatabaseModel.KEY_PARAM1;
// ...
}
The issue I'm experiencing is that at runtime, when I call localStore.function()
, temp is being assigned null, as InternalModelInterface.COLUMN_PARAM1
is null. Does this make sense? Shouldn't InternalModelInterface.COLUMN_PARAM1
be evaluated at compile time and inlined?
This is for an Android application. Thanks in advance.
I'll further explain to clarify any confusion.
Objects of the DatabaseModel
class are instantiated as a JSON response is parsed. The constants defined in the DatabaseModel
class represent the keys to look for in the JSON response.
The InternalModelInterface
defines the column names used in the local (cache) database on the device. For several reasons (including they keys being illegal column names in SQLite), I'm not reusing the keys as column names.
The reason I'm using an interface and not just a plain class is that the interface also specifies required methods that need to be implemented by the third class, LocalStore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JLS3 §8.3.2.1、§9.3.1 http:// /java.sun.com/docs/books/jls/third_edition/html/classes.html#38010
因此,在您的示例中永远不应观察到
null
。那么这是一个Android bug。JLS3 §8.3.2.1, §9.3.1 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#38010
So
null
should never be observed in your example. It's an Android bug then.我不是 android 专家,但我认为如果你不创建该类的实例,它会在编译时优化。如果您为 DatabaseModel 创建一个构造函数并在某个地方实例化它,它似乎可以为我解决这个问题。
I'm not and android expert but I think that if you don't create an instance of the class, it's optimised out at compile time. If you create a constructor for DatabaseModel and instantiate it somewhere it seems to solve this for me.