Sign in to follow this  
Thygrrr

[java] Reseating References in J2ME

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this