This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Chapter 9 Threads Self Test Question no. 16 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Chapter 9 Threads Self Test Question no. 16" Watch "Doubt in Chapter 9 Threads Self Test Question no. 16" New topic
Author

Doubt in Chapter 9 Threads Self Test Question no. 16

waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 05, 2010
Posts: 67

Can you post the code otherwise it is difficult to answer as it is mentioned by Seetharaman Venkatasamy


OCPJP 6 | OCEJWCD 6
srikanth kasa
Greenhorn

Joined: Aug 28, 2012
Posts: 2
"It is better to keep your program.we will clarify your doubt".
waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

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

Joined: Aug 11, 2012
Posts: 370
    
    1

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

Joined: Apr 17, 2012
Posts: 46

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 17, 2012
Posts: 46

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Chapter 9 Threads Self Test Question no. 16
 
Similar Threads
Why static variables and not allowed inside a method ?
Illegal modifier for parameter y, only final is permitted
What is meant by a 'virtual method'?
Is there a better way to do this method...?
difference between static local variables and static global variables.