在编译时未评估的接口中定义的最终静态字符串 - Android

发布于 2024-11-09 04:36:10 字数 1059 浏览 0 评论 0原文

我有两个类和一个接口(例如 DatabaseModelLocalStore 和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2024-11-16 04:36:10

JLS3 §8.3.2.1、§9.3.1 http:// /java.sun.com/docs/books/jls/third_edition/html/classes.html#38010

在运行时,静态变量
最终并且初始化为
编译时常量值为
首先初始化。这也适用
接口中的此类字段(第 9.3.1 节)。
这些变量是“常量”
永远不会被观察到有他们的
默认初始值(§4.12.5),甚至
通过狡猾的程序。

因此,在您的示例中永远不应观察到 null 。那么这是一个Android bug。

JLS3 §8.3.2.1, §9.3.1 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#38010

at run time, static variables that are
final and that are initialized with
compile-time constant values are
initialized first. This also applies
to such fields in interfaces (§9.3.1).
These variables are "constants" that
will never be observed to have their
default initial values (§4.12.5), even
by devious programs.

So null should never be observed in your example. It's an Android bug then.

一萌ing 2024-11-16 04:36:10

我不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文