0

Our intents carry data from one activity to another by key value pairs called extras.

We initialize the key (i.e. declare it as a constant and assign it something e.g. public static final String mykey= "something";) before passing it to the intent by using intent.putExtra(mykey, myvalue);

My question is why do we need to assign a value to the key when it is being declared? What is the use of that value? What is the use of ' = "something" ' in public static final String mykey= "something";

I posted a related question, and a respected person (respected because of their valuable answers) said that when a final is declared, a value must be assigned so it is known what the constant is. Sounds like common sense.

But if I simply declare a constant public static final String a; the compiler does not complain at all, which means initializing a final variable with a value is not a must, as long as it is initialized before it is used.

A relevant answer is appreciated. Thank you in advance.

user2882662
  • 535
  • 1
  • 6
  • 18
  • `final` means it can be assigned once, and only once. It does not define where that assignment happens. You can assign is when you declare it, or assign it on first use. It makes no difference. Trying to assign it twice or more is an error. As I said in your last question, you are confusing key names, which are simply a string - any string, and Java modifiers. The two things are not related in any way. I repeat, the key name is a string, you can give it any value you like. How you give it that value is not defined and is entirely up to you. – Simon Nov 03 '13 at 14:23
  • The final modifier indicates that the value of this field cannot change. http://stackoverflow.com/questions/13538637/why-final-field-variable-can-not-assign-to-blank-in-java and also check this http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2 – Raghunandan Nov 03 '13 at 14:29
  • http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4 – Raghunandan Nov 03 '13 at 14:32
  • My only question, which I have been asking previously as well, is that why assign a value? What is the use of that value? – user2882662 Nov 03 '13 at 14:45
  • 1
    @user2882662 check the java docs in my previous comments.you need to initialize final variable . the use is you will use the key the same key to retrieve the string in another activity – Raghunandan Nov 03 '13 at 14:47
  • Your question is still confusing. Are you asking why a final static string must be assigned a value? Or are you asking why a key value pair name must have a value? – Simon Nov 03 '13 at 15:09
  • Thank you very much. Your answer as well as this [link](http://stackoverflow.com/questions/11345061/why-must-a-final-variable-be-initialized-before-constructor-completes) helped. Thank you. – user2882662 Nov 03 '13 at 15:13
  • @Simon: Former! I got the answer, from Raghunandan and this [link](http://stackoverflow.com/questions/11345061/why-must-a-final-variable-be-initialized-before-constructor-completes) Thank you for answering, and your previous answers as well. – user2882662 Nov 03 '13 at 15:19

1 Answers1

1

I'm assuming an Intent is backed by a Map.

If you would have an uninitialized variable as the key, this would mean that the value is essentially lost: there's no way of retrieving it since there's no key associated with it (although I believe it might not be possible at all to insert a null key in a map).

You don't have to actually assign this key to a variable: intent.putExtra("somekey", somevalue); works just as fine.

It's just a matter of making sure you don't accidentally use the wrong key.

As an illustration of why it is beneficial to use final variables:

public static void main(String[] args) {
    Map<String, Integer> someMap = new HashMap<>();
    String theValue = "X";
    someMap.put(theValue, 5);
    System.out.println("Variable: " + theValue);
    System.out.println("Map: " + someMap.get(theValue));
    
    theValue = "Y";
    System.out.println("Variable: " + theValue);
    System.out.println("Map: " + someMap.get(theValue));
    System.out.println("ByValue: " + someMap.get("X"));
}

Output:

Variable: X
Map: 5

Variable: Y
Map: null
ByValue: 5

If theValue would be final, it wouldn't be able to be reassigned and there wouldn't be any problems getting the value from the underlying Map.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170