File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String variables in static method

 
vidhya suvarna
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source:
]http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_2.php

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?
A1 Local Instance
A2 Local New Instance
A3 Loca Add New Instance
A4 Compiler Error

The correct ans is Local New Instance.

Can someone tell me why the value of s updated in method() doesnt affect it when it's displayed?

[ September 17, 2008: Message edited by: vidhya suvarna ]
[ September 17, 2008: Message edited by: vidhya suvarna ]
 
sannuth kashikar
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • 0
  • 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();
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);
}
}
}
}

In these kind of questions, just remember that local variables have priority over other variables.

s = "New Instance";//This is Static variable

String s = "Local";//This is local variable

method(s); //Local variable take precedence over static, so local variable is sent to the function.

and the function modifies the local variable(The variable local to this function)
 
vidhya suvarna
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change the occurrence of statements:
s = "New Instance";
String s = "Local";

as

String s = "Local";
s = "New Instance";

you will find that static variable is passed to the method.

My query is s is a String variable(object) so if we update its value in a method wouldnt it be reflected when it returns from the method?
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my take on things.


[ September 17, 2008: Message edited by: Stephen Davies ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen i got it now!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic