Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static String+= (concatentation )...

 
kumaresan Natarajan
Greenhorn
Posts: 27
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Its prints
New InstanceAdd
Local
New Instance


my doubt is here when i calling method(String s) by passing static String its print New InstanceAdd its ok ...
after i am tring to print static variable s , its print newInstance
why its print like this instant of New InstanceAdd ???
here what the mean of static ???


Thanks in advance
kumaresan
[ July 28, 2006: Message edited by: kumaresan natarajan ]
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable, meaning you can cannot change the value of them.

When you call 'method()', you pass it a COPY of the static reference (still refering to the same String object). However, you then re-assign the value that it refers to. i.e. s+="Add";

The original static reference still refers to the 'newInstance' String, and the copy of the original reference now refers to the newly created 'newInstanceAdd' String.

One more thing, this example does try to confuse you by 'shadowing' reference variables (giving the same name to multiple references - their scope determines which one you access unless you use keywords like 'this' or use the class name.

[ July 28, 2006: Message edited by: Andy Morris ]
[ July 28, 2006: Message edited by: Andy Morris ]
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok how many objects are created in above piece of code
According to me there are 5 objects created as follows

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are hijacking, this topic. If you want to change the question, then please start a new thread.
[ July 28, 2006: Message edited by: Barry Gaunt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic