Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How this program compiled & outputs after modifying variable with "final" prefix?

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i tried to run this class I got o/p as :- "Now original is modified"
Here I am modifying variable "style" in modify method which is of type "final".

public class TestQ24 {

public static final StringBuffer style =
new StringBuffer("original");
public static void main (String[] args){
TestQ24 tq = new TestQ24();
tq.modify( style );
System.out.println("Now " + style );
}
public void modify( StringBuffer sb ){
sb.append(" is modified" );
}
}

Please explain me why i am getting above o/p though i am modifying "final" variable.
Thank you.
Pras
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when a reference variable is declared final ,it means that the same variable can not be re-assigned to refer to other object.
The internal state of the object refered by the final variable can be changed.
In //2 you are modifying the internal state of the object origianally refered by style.Reassigning style to other object would have given you the compilation choke.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I always wondered if there is a way to declare objects pointed to to be final. Something like const pointers to const variables in c++.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Johnson:
By the way, I always wondered if there is a way to declare objects pointed to be final. Something like const pointers to const variables in c++.



Can't we declare an object final and refer to it with final variable ?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for object declared as final

we can't assign new instatnce of it.But we can change it 's parametrs value.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the object is mutable, one approach might be to clone the object, so that you can keep the original insulated from modification.

See Cloning objects from "Appendix A" of Bruce Eckel's Thinking in Java (3rd ed.).
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic