• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

plz explain the output......

 
arivu mathi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic