[java] Reseating References in J2ME

Started by
5 comments, last by Thygrrr 18 years, 8 months ago
Hello. As far as I know Strings in Java (J2ME in particular) are immutable. How do I make sure that all references to a given String are updated once I want that string replaced with another (mainly for internationalization - when the user chooses a different language, I want all String references in the app to point at the right new language versions without having to manually set them all). I'm stumped. I'd kill for pointers now. How do I do it?
Advertisement
I'm afraid it isn't possible. You're going to have to reset them all manually.
The only alternative is to store the strings in Objects that aren't final, like a StringBuffer or a wrapper object of your own, but that's going to have considerable overhead.

shmoove

EDIT: Well, considerable might be a bit exaggerated, I guess it depends on the application. But when you're working on constrained devices every bit counts.
It does beg the question though, why do you have so many different references to the same strings? Couldn't you just have one reference per string and use it globally?

shmoove
Making a StringPtr class might not be too bad.

You could also organize your strings like this:

class Strings{  public static String HI;  public static String PRESS_A_KEY;  //...  static void setEnglish()  {    HI = "Hi";    PRESS_A_KEY = "Press a key";    // ...  }  static void setFrench()  {    HI = "Salut";    PRESS_A_KEY = "Pressez une keyez";    // ...  }}


and when you need "hi", you reference it Strings.HI
Yes, that's what I'm doing now.

However, this doesn't scale well at all with my GUI framework. It happens when you create several buttons with text in various menus and NOT hardcoding them to use certain strings.

What I WANT to do is comfortably add a text file to my JAR and immediately have an additinal language version available without needing to touch the source again.

This is more a matter of taste than a matter of necessissity, but I want to create a centralized, I18N-capable GUI framework accompanied by a template MIDlet for our software developer team.

However, I18N is where I hit rock bottom, because I can't seem to think of an efficient way to feed fresh strings into an already built menu structure. And I also don't want to force the user to "restart the app to apply the changes", which I find is an insult to the user.
Then I guess you could use the string wrapper approach.
You make a class like this:
public class StringWrapper {  public String theString;}

and replace any use of Strings in your framework with StringWrappers, dereferencing theString any time you're about to paint them.

shmoove
This is so ingeniously simple, it's simply ingenious! DUH!!!

Thanks shmoove. I was so hooked up in "clean OO with getter and setter methods only" that I forgot about good ol' structs and classes with public members. This'll do fine :)

This topic is closed to new replies.

Advertisement