Midlet如何调用静态变量?

发布于 2024-08-02 23:42:32 字数 985 浏览 1 评论 0原文

我有一个 midlet,它有一个静态变量。我需要保留在此变量中创建的所有实例的记录。但它不像静态变量那样工作。我的代码段看起来像这样。我在 sun wireless toolkit 2.5.5 上运行这个 midlet。我可以从该工具包创建相同 midlet 的许多对象,但我的计数器仍然只显示 1 个。

public class SMS extends MIDlet implements CommandListener {

   private Display display;
   private TextField userID, password ;
   public static int counter ;

   public SMS() {

      userID = new TextField("LoginID:", "", 10, TextField.ANY);
      password = new TextField("Password:", "", 10, TextField.PASSWORD);
      counter++;

   }

 public void startApp() {

      display = Display.getDisplay(this);
      loginForm.append(userID);
      loginForm.append(password);
      loginForm.addCommand(cancel);
      loginForm.addCommand(login);
      loginForm.setCommandListener(this);
      display.setCurrent(loginForm);

  public void commandAction(Command c, Displayable d) {

     String label = c.getLabel();
     System.out.println("Total Instances"+counter);

每次,计数器只显示创建的 1 个对象。

I have a midlet which has got a static variable. I need to keep the record of all the instances created in this variable. but it does not work like static variable. my code segments look like this. I am running this midlet on sun wireless toolkit 2.5.5. I can create many objects of same midlet from that toolkit but still my counter shows only 1.

public class SMS extends MIDlet implements CommandListener {

   private Display display;
   private TextField userID, password ;
   public static int counter ;

   public SMS() {

      userID = new TextField("LoginID:", "", 10, TextField.ANY);
      password = new TextField("Password:", "", 10, TextField.PASSWORD);
      counter++;

   }

 public void startApp() {

      display = Display.getDisplay(this);
      loginForm.append(userID);
      loginForm.append(password);
      loginForm.addCommand(cancel);
      loginForm.addCommand(login);
      loginForm.setCommandListener(this);
      display.setCurrent(loginForm);

  public void commandAction(Command c, Displayable d) {

     String label = c.getLabel();
     System.out.println("Total Instances"+counter);

everytime, counter shows only 1 object created.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无妨# 2024-08-09 23:42:32

我见过的唯一允许静态变量在应用程序的“调用”之间保留的系统是 Android。我从未见过在 MIDlet 调用之间维护静态数据的 J2ME 设备。但是,MIDlet 套件中的 MIDlet 可以共享静态数据,如此处所述,当其中至少一个正在运行时。

如果要在 MIDlet 调用之间维护数据,则需要使用 javax.microedition.rms 中的记录存储 API,它提供对持久存储的访问。

The only system I've seen that allows static variables to remain between 'invocations' of an application is Android. I've never once seen a J2ME device that maintains static data between invocations of a MIDlet. However, MIDlets within a MIDlet suite can share static data, as described here, while at least one of them is running.

If you want to maintain data between invocations of a MIDlet, you need to use the Record Store APIs in javax.microedition.rms, which provide access to a persistent store.

苏佲洛 2024-08-09 23:42:32

您的 MIDlet 仅实例化一次。有点儿。

MIDP 运行时可能不允许您启动同一个 MIDlet 两次,只要它已经在运行。

如果退出 MIDlet,计数器将返回到 0,因为它仍然是 RAM 中的值,并且 Java 虚拟机进程将终止。

在某些 Nokia series40 手机上,JVM 进程永远不会终止,因此您可以使用它来显示自上次打开手机以来 MIDlet 被创建的次数。

静态变量存储在 JVM 内存中的 Class 对象中。您需要了解类加载(以及 J2ME 中通常缺乏对类卸载的支持)才能弄清楚可以在静态变量中存储什么。

我建议将 counter++; 移至 startApp(),因为每次将 MIDlet 带到前台时都会调用它。

这还允许您将计数器存储在 RMS 记录中,以提高准确性。

Your MIDlet is only instantiated once. Kind of.

The MIDP runtime will probably not allow you to launch the same MIDlet twice as long as it is already running.

If you exit the MIDlet, counter goes back to 0 because it is still a in-RAM value and the Java Virtual Machine process is terminated.

On some Nokia series40 phones, the JVM process is never terminated so you could use this to show how many times the MIDlet was created since the last time the phone was switched on.

Static variables are stored in a Class object in the JVM memory. You need to understand class loading (and the usual lack of support for class unloading in J2ME) to figure out what you can store in static variable.

I would suggest moving counter++; to startApp() as it could be called everytime the MIDlet is brought to the foreground.

That would also allow you to store counter in an RMS record for additional accuracy.

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