Forums Register Login

local scope of String variable

+Pie Number of slices to send: Send
1)t.nameTest(sName); should pass sName value of "good" to nameTest(). Inside nameTest(), sName reference should point to new String "good idea" in heap.

But output is "good" . Why is scope of sName considered local to method, when its static and there is no other declaration of sName string in any other method ? So without declaration of sName in nameTest(), how come sName is not pointing to new String object "good idea"

2) Will start() execute run() or not. If start() executes run(), then sNmae should become "good idea 0 1 2 3" after execution of run(). Otherwise sName should have value of "good idea". So, if Thread.start() is not used, does run() get executed ?
+Pie Number of slices to send: Send
 

nirjari patel wrote:1)t.nameTest(sName); should pass sName value of "good" to nameTest(). Inside nameTest(), sName reference should point to new String "good idea" in heap.



Correct. The local variable in the nameTest() method will end up pointing to a "good idea" string object.

nirjari patel wrote:But output is "good" . Why is scope of sName considered local to method, when its static and there is no other declaration of sName string in any other method ? So without declaration of sName in nameTest(), how come sName is not pointing to new String object "good idea"




The static variable points to a "good" object. The nameTest() method doesn't change that. So, why do you expect it to point to something different?

Henry
1
+Pie Number of slices to send: Send
 

nirjari patel wrote:
2) Will start() execute run() or not. If start() executes run(), then sNmae should become "good idea 0 1 2 3" after execution of run(). Otherwise sName should have value of "good idea". So, if Thread.start() is not used, does run() get executed ?



The start() method will start a new thread, and it is this new thread that will call the run() method. Of course, there is a race condition -- the main thread is returning from the nameTest() method, and then print the static variable SName, while at the same time the run() method is running -- So what prints will depends on what the run() method did so far.

Henry
+Pie Number of slices to send: Send
 

nirjari patel wrote:... and there is no other declaration of sName string in any other method ?



This part of your problem statement is false. If it were true you could not see the output you do. But the statement is wrong and so the output is the expected behavior.
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 672 times.
Similar Threads
Global Variables
String/Thread question
Thread Question from "Java Certifications" SCJP 1.6 Mock Exam Practice Questions
static variable remains unchanged
String immutability
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:07:50.