This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "code" Watch "code" New topic
Author

code

hemamalini s.
Greenhorn

Joined: Oct 17, 2005
Posts: 13
Q14 public class Test14{
static String s ="Instance";
public static void method(String s){
s+="Add";
}
public static void main(String a[]){
Test14 t = new Test14();
s = "New Instance";
String s = "Local";
method(s);
System.out.println(s);
System.out.println(t.s);
}
}
What is output?
output is locan new instance.
How?
Thanx
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
The code below should explain the concept.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Mandy Hou
Greenhorn

Joined: Oct 15, 2005
Posts: 21
there is a copy of refence passed into call method.
So both this.s and local s are not changed


Regards<br />Mandy<br /> <br />-----------------<br />Thinking,Thinking,Thinking,<br />Before you do any action!!!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

i believe the complete explanation goes as follows:
when you pass an object to a method you are passing a copy of the value of an original variable. this value contains a bit pattern of a way to refer to an object. So: you can change the object's members by both: the argument in the method and the original variable, but not let the original value point to another object (you can let the argument refer to another object).

because strings are immutable, you can't change them and always a new string is created, so the argument refers to the new string, but the original variable still refers to the original string


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
hemamalini s.
Greenhorn

Joined: Oct 17, 2005
Posts: 13
Thanx everybody i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code
 
Similar Threads
String variables in static method
Please explain me answer. (static)
can anyone explain this
String operation
plz explain the output......