• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Chapter 9 Threads Self Test Question no. 16

 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I really can't understand the relation to static here, the answer says "If c were static, then because yack is synchronized, answers
C and E would have been correct." Can anybody explain how???
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post complete question(including code) . so that it is easy to understand.
because here some people like me may have different version of book or even dont have book.

Cheers
 
Abhilash Sharma
Ranch Hand
Posts: 67
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code otherwise it is difficult to answer as it is mentioned by Seetharaman Venkatasamy
 
srikanth kasa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"It is better to keep your program.we will clarify your doubt".
 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is Q. 16 from the Threads Chapter with answer as well:


Which are true? (Choose all that apply.)
A. Compilation fails
B. The output could be 4 4 2 3
C. The output could be 4 4 2 2
D. The output could be 4 4 4 2
E. The output could be 2 2 4 4
F. An exception is thrown at runtime
Answer:
F is correct. When run() is invoked, it is with a new instance of ChicksYack and c has
not been assigned to an object. If c were static, then because yack is synchronized, answers
C and E would have been correct.
A, B, C, D, and E are incorrect based on the above. (Objective 4.3)

I went with the options C and E But I can't understand why c doesn't get initialized before an instance of ChicksYack call run() even though the code to initialize c is written before those two ChicksYack statements.

Thanks in advance,
Waq
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waleed qureshi wrote:Hi - I really can't understand the relation to static here, the answer says "If c were static, then because yack is synchronized, answers
C and E would have been correct." Can anybody explain how???


static variables are class variables.. Once it has been initialized, it will be done for all the objects of that class..
This is not the case with instance variables.. So, every time you create new object, these variables are given default value for that instance..

Now analyze the problem and see whether you can relate the above two sentences with the code.
 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
static variables are class variables.. Once it has been initialized, it will be done for all the objects of that class..
This is not the case with instance variables.. So, every time you create new object, these variables are given default value for that instance..


OK I got that. But my main doubt here is about why does it say 'c doesn't get initialized' even though the code to initialize c is written before the calls to rum from new instances of ChicksYack???
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are starting threads on new instances(ChicksYack) which dont initialize c. you are initialized the c in different thread(in Main thread)got it?
think about below fragment...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have indented your code and applied code tag, how cool is that now to look.. Please follow the same in future
 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah got it now but after much thoughts. Its quite a complex concept at first sight. And yeah thanks for the code indenting, I didn't know about that option.

Thanks again,
Waq
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic