• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to know why this doesn't give error:




It prints "Aadd1add2"

Thanks.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...because it is legal Java language syntax.
Be more specific.
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if I declare it final it will change? Why is it possible?

Thanks.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to guess that Kalyani meant "why am I allowed to modify an object that is declared to be final?".

What is final in this case is not the object itself, but the object reference, meaning that you can't reassign the variable "a" to reference some other StringBuffer. Declaring the reference variable to be final does not make the object to which the reference points immutable.

-- Jon
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declared object as final you can't reassign another object reference to the object.But we can change the instance member values.


final StringBuffer a = new StringBuffer("A");
final StringBuffer b = new StringBuffer("B");
b=new StringBuffer("BBBB"); // It shows error message
a.append("add1");
a.append("add2");
System.out.println(a );


Here object a reference can't be changed. so it doesn't show any error messages.
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got the point.

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic