• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manipulating final variable in java

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm novice java programmer.

I know that you cannot change final variable in java.
But in Head First Servlet and JSP i read somewhere that "you can change final variable programatically unless it is immutable."


Can any one please explain me with small code if posseible???

Thanks in Advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,
I think they mean that you can change the value of the object referenced but not the pointer itself.

Suppose I have the following two variables:
final String myString = "test";
final List<String> myList = new ArrayList<String>();

Now I cannot assign a new value to myString or myList since both are immutable. I can do myList.add("test") which changes the value of the object. I can't do that with myString because the String class is immutable. There are no methods on the String class that change it's value.
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Thanks a lot for your reply!!!

I partailly understood from your reply. Yes i completely agree the case with the Sring class as it is immutable.

But from your quote

"you can change the value of the object referenced but not the pointer itself."

From above statement (Except String class) i can change the value of that perticular object, but i cannot change the location(pointer) the object points to.

Is my understand absolutly right?

Thanks in advance.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jay:
From above statement (Except String class) i can change the value of that perticular object, but i cannot change the location(pointer) the object points to.


Yes Jay, you're right on the money.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic