File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static String+=  (concatentation )... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static String+=  (concatentation )..." Watch "Static String+=  (concatentation )..." New topic

Static String+= (concatentation )...

kumaresan Natarajan

Joined: Jul 23, 2005
Posts: 27

Its prints
New InstanceAdd
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
[ July 28, 2006: Message edited by: kumaresan natarajan ]

Thanks and Regards
kumaresan N
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
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

Joined: Feb 07, 2005
Posts: 643
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Static String+= (concatentation )...
It's not a secret anymore!