Prabha Vathi
Prabha Vathi

Reputation: 347

Is it possible to change the value of a final variable (Android)?

(I know final can't be changed. But if you read the example I have typed, you can understand why I am asking this question!)

I am reading the example hello world Android app:

MainActivity.java

public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";
....
....
intent.putExtra(EXTRA_MESSAGE, message);

How does it work? Are we storing the message value in EXTRA_MESSAGE?

Additionally, how many SharedPreferences can I have to store data? Assuming that, my app needs to store the levels and high scores, how do I do that?

public final static String HIGH_SCORE = "com.example.helloworld.HIGH_SCORE";
public final static String LEVELS = "com.example.helloworld.LEVELS";

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

How do I change the above lines to save the levels and scores?

Upvotes: 2

Views: 2997

Answers (6)

Rachit Mishra
Rachit Mishra

Reputation: 6112

  1. Can I change a final variable?

As you have said, and as you know this too, we can't change final values normally, but using a reflection API this may be possible, see this an answer to Stack Overflow question Change private static final field using Java reflection.

  1. How is this stored intent.putExtra(EXTRA_MESSAGE, message); ?

As many of the answers have said, putExtra works in a way of WAP, where a specific key is mapped to a specific value of type string, integer, etc. So, in this case EXTRA_MESSAGE acts as the key, while the message acts as value.

  1. How many values can I add to SharedPreferences?

AFAIK, there is no limit on how many SharedPreferences you can add, it depends on you, but as SharedPreferences are mostly related to application settings, although, not explicitly required, but we can opt for storing large or personal application data to an XML file or to databases.

  1. How can I save my code to store a specific value?

    editor.putInt(getString(R.string.saved_high_score), newHighScore);
    

    Assuming newHighScore is the value you want to store, you may opt to change getString(R.string.saved_high_score) to a more specific key like Level Number, so that when you next time update it, the score will automatically get overwritten.

Upvotes: 0

Mohit
Mohit

Reputation: 11324

EDIT 1:

Your question is too different from the original question. This is because you are just misunderstanding it.

MainActivity.java

public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";
....
....
intent.putExtra(EXTRA_MESSAGE, message);

This piece of code does not mean that EXTRA_MESSAGE is storing message now. It's something like a key/value pair.

It means:

Intent is storing the value of a message with a key named value of EXTRA_MESSAGE, so if you pass a value of EXTRA_MESSAGE to an intent, it will give a value to message.

final is fixed. It never, never changes its value, so that it's called FINAL.

And nor you can change it. It is always treated as a constant.

See final (Java) on Wikipedia for more help.

Upvotes: 0

krishan711
krishan711

Reputation: 873

An Intent has a Bundle within it, which holds parameters to be passed on. Think of this as a mapping, so when you write

intent.putExtra(EXTRA_MESSAGE, message);

it saves message into a field named by EXTRA_MESSAGE.

So, if you want to store the highscore and level use:

public final static String HIGH_SCORE = "com.example.helloworld.HIGH_SCORE";
public final static String LEVELS = "com.example.helloworld.LEVELS";

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(HIGH_SCORE, newHighScore);
editor.putInt(LEVELS, level);
editor.commit();

Note that if you write into HIGH_SCORE or LEVELS again, it will override the current value!

Upvotes: 0

Damian Jeżewski
Damian Jeżewski

Reputation: 1246

This line:

intent.putExtra(EXTRA_MESSAGE, message)

means that you're storing the message's value in the Intent's internal map under the EXTRA_MESSAGE key. It does not change in any way content of the EXTRA_MESSAGE variable.

Upvotes: 0

Saj
Saj

Reputation: 18712

No you cannot change the value once assigned. That is why it's called final. You can have many preferences as you want as they are just files.

intent.putExtra(EXTRA_MESSAGE, message);

Think of a map. It's just a key-value relationship. EXTRA_MESSAGE is the key and message is the value.

To put, you do-

preferences.edit().putString("name", "adam");

To get, you can use the key you used-

preferences.getString("name", "none"); 

Tutorials and References:

Shared Preferences

Vogella

Upvotes: 0

Fritz
Fritz

Reputation: 10055

When in doubt, check the docs:

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

What's defined here:

public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";

is a constant meant to remain like that, otherwise, you can see the bugs comming here:

intent.putExtra(EXTRA_MESSAGE, message);

if Extra_MESSAGE were to change, the Entry that has its previous value as a key could end up "lost" (inaccessible, to be more precise, because you didn't save that value somewhere else).

In order to save further info, you'll need more keys for the map. This means that your correctly defined fields here:

public final static String HIGH_SCORE = "com.example.helloworld.HIGH_SCORE";
public final static String LEVELS = "com.example.helloworld.LEVELS";

Can be used to store those values in the given map, or wherever you want to use them as a identifier (bases on a key-value structure). The concept here is that you have to define a constant accessible through the game in order to be able to store AND retrieve the data.

Upvotes: 3

Related Questions