aspose file tools*
The moose likes Beginning Java and the fly likes Manipulating final variable in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Manipulating final variable in java" Watch "Manipulating final variable in java" New topic
Author

Manipulating final variable in java

Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
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

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating final variable in java
 
Similar Threads
Question on final
5 Theoratical questions core java
Semantics of "final" keyword - reg.
final variables can be manipulated ...found this in SCWCD book by Bert!
Declare a constant array of constant int in Java.