This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings information required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings information required" Watch "Strings information required" New topic

Strings information required

Bilal Ali
Ranch Hand

Joined: Jun 14, 2007
Posts: 66
Dear All,
What and why will be the result of the following piece of code:

String s = "Test1";

public void testFunction(String s)


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Bilal Ali wrote:
What and why will be the result of the following piece of code:

please use code tag to post your code. and also post the program with good format . Coming to your question , you will get the answare "Test1" due to shadowing of the variable
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
The output will be "Test1"

Because in sysout the variable referred is a global variable and not local 's' declared inside the function.

My Techie Blog - Java Questions
Rob Spoor

Joined: Oct 27, 2005
Posts: 20077

How To Ask Questions How To Answer Questions
Clyde DeSouza

Joined: Mar 16, 2009
Posts: 26
No need for call by reference and call by value

put your code inside proper format anyways i will give you the answer i guess you wanted to refer to class variable but instead your referring to method variable you could have refered to class variable if you used the different name over here but you used the same name as method name to refer to class variable in such situation just use
this.<variable name>=<local method variable name>
in your case here

this refers to current object and you are saved from ambiguity! got it any more queries do post it here
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43923
"Joywish Man" welcome to JavaRanch
Clyde DeSouza

Joined: Mar 16, 2009
Posts: 26
Thanks a lot !
James Mathews

Joined: Mar 16, 2009
Posts: 7
how this is refering to current object?
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Strings information required
jQuery in Action, 3rd edition