This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static String+=  (concatentation )..." Watch "Static String+=  (concatentation )..." New topic
Author

Static String+= (concatentation )...

kumaresan Natarajan
Greenhorn

Joined: Jul 23, 2005
Posts: 27



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 ]

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: http://aspose.com/file-tools
 
subject: Static String+= (concatentation )...