• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible?

 
mohanasundaram muthukannan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic