• Post Reply Bookmark Topic Watch Topic
  • New Topic

local scope of String variable  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!