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 code 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 "code" Watch "code" New topic


hemamalini s.

Joined: Oct 17, 2005
Posts: 13
Q14 public class Test14{
static String s ="Instance";
public static void method(String s){
public static void main(String a[]){
Test14 t = new Test14();
s = "New Instance";
String s = "Local";
What is output?
output is locan new instance.
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

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

Joined: Jul 19, 2004
Posts: 8403

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
hemamalini s.

Joined: Oct 17, 2005
Posts: 13
Thanx everybody i got it
I agree. Here's the link:
subject: code
It's not a secret anymore!