| Author |
Is it possible?
|
mohanasundaram muthukannan
Ranch Hand
Joined: May 29, 2008
Posts: 35
|
|
i wrote pne program using final keyword. A final variable is immutable; it can only be assigned to once then class Example{ public static void main(String args[]) { final int int_array[] = new int [3]; int_array[0]=5; int_array[1]=5; int_array[2]=5; for(int i: int_array) { System.out.println(i); } System.out.println("After modify"); int_array[0]=6; int_array[1]=6; int_array[2]=6; for(int i: int_array) { System.out.println(i); } } } // output 5 5 5 After modify 6 6 6 how is it poosible? Any body explain this code.. Thanks, Mohan
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
i wrote pne program using final keyword. A final variable is immutable; it can only be assigned to once
You are only assigning a value to this variable once. The above is invalid. The final modifier prevents this reassignment of the variable intArray to the new array with four elements. However, changing the contents of the array itself is fine. The only way you can prevent the value of an object assigned to a final variable changing is to make the object you assign immutable. For example, if your final variable pointed to an Integer there would be no way to change its value. (BTW: please check your private messages)
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3026
|
|
Originally posted by mohan m: A final variable is immutable; it can only be assigned to once
This statement is not true. A final variable can only be assigned to once, but that does not make it immutable.
|
Steve
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24045
|
|
Originally posted by Steve Luke: that does not make it immutable.
Given that the dictionary definition of "immutable" is "not capable of or susceptible to change", I'd be interested in hearing you defend this statement! I would posit that marking a variable final accomplishes precisely that, by definition. Now, making objects immutable is another story altogether, but that's not what we're talking about.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3026
|
|
Originally posted by Ernest Friedman-Hill: Given that the dictionary definition of "immutable" is "not capable of or susceptible to change", I'd be interested in hearing you defend this statement! I would posit that marking a variable final accomplishes precisely that, by definition. Now, making objects immutable is another story altogether, but that's not what we're talking about.
OK, lexically it may be correct, though not in the way the OP thought it had. But just to be as pedantic as you: Output: null java.lang.Object@187aeca Oops. The final variable changed from being null to pointing at an Object, which means it is "capable of or susceptible to change". [ July 28, 2008: Message edited by: Steve Luke ] [ July 28, 2008: Message edited by: Steve Luke ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24045
|
|
The variable here isn't final, it's static, and if you mark it "final" instead it won't compile. But I know what you're saying. I think it's possible to get some version of this idea to run (inspecting a final variable before it's set), but off the top of my head I don't remember how. Even without that trick, it's possible to use reflection to change the value of a final variable, showing that they're really not "immutable" at all.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3026
|
|
Originally posted by Ernest Friedman-Hill: The variable here isn't final, it's static, and if you mark it "final" instead it won't compile.
That was stupid of me... I really didn't expect to be able to use the initializer block but didn't second guess when it worked...
Originally posted by Ernest Friedman-Hill: But I know what you're saying. I think it's possible to get some version of this idea to run (inspecting a final variable before it's set), but off the top of my head I don't remember how. Even without that trick, it's possible to use reflection to change the value of a final variable, showing that they're really not "immutable" at all.
Right, I just wanted to show that the variable exists, has an initial value of null, and that initial value is changed with the assignment. I was hoping it would be easy to show but now I think you have to resort to Reflection trickery to get around the compile errors. [ July 28, 2008: Message edited by: Steve Luke ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24045
|
|
OK, here we go. This is paraphrased from Puzzle 49 of Bloch and Gafter's "Java Puzzlers." This prints "1", because the object in INSTANCE is constructed before the value of CURRENT_YEAR is calculated. It relies on the fact that static variables in a class are initialized in lexical order.
|
 |
 |
|
|
subject: Is it possible?
|
|
|