Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain this question to me? please  RSS feed

 
Andrew Symantec
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above question is excerpted from mock green's SCJP exam. The resut of this question is "ABC".
I don't understand it, since the instance of class A is already created by using new A(sa),
and the array is alredy passed in, the constructor of A should initialize its own string array array. How could the value be changed?
Thank you very much in advance.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you only have 1 sa object. Both objects A and B have their own reference to that one object but, there can be only one.
Now, the real point of this is to show how using the synchronized keyword affects the behavior in multithreaded applications. Since B has synchronized on sa, the Thread A you start can not perform operations on the sa object until B releases it (ie the synchronized block ends).
 
Andrew Symantec
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I understand it now.
Thanks Carl
BTW, you are not from Austria, are you?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Their are probably several points tied into what you said.

1. Passing an array reference seems different than passing a regular reference. The assignment being done is to what the reference referrs, and not to the reference. So the reference keeps its value, but what the reference points to is changed. Sort of like a pointer to a pointer if you are from a pointer coding background.
2. Though the thread is started it can not proceed until the original thread releases the lock that it needs. This often burns people when they start using SwingUtilities InvokeLater but fail to grasp that point.
3. Ugh, I can't remember the 3rd point
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!