File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes plz explain the output...... 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 "plz explain the output......" Watch "plz explain the output......" New topic
Author

plz explain the output......

arivu mathi
Ranch Hand

Joined: Jun 26, 2006
Posts: 38
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?

Local New Instance
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
s = "New Instance"; //set class variable (static String) s
String s = "Local"; //define a s shadow class variable s
method(s); //pass s reference by value which points to "Local" String Object
System.out.println(s); //print local variable s
System.out.println(t.s);//print class variable s

Inside the method(String s) method, changing string does not affect to s in main method.
[ June 26, 2006: Message edited by: wise owen ]
 
Consider Paul's rocket mass heater.
 
subject: plz explain the output......