• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Chapter 9 Threads Self Test Question no. 16

 
Ranch Hand
Posts: 46
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 67
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code otherwise it is difficult to answer as it is mentioned by Seetharaman Venkatasamy
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"It is better to keep your program.we will clarify your doubt".
 
waleed qureshi
Ranch Hand
Posts: 46
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic