This seems obvious but I want to make sure that I’m doing this right. If I try to attain a lock on a null reference this will not work correct? Because it uses a property of an object not a reference?
ex
Object lock = null;
synchronized(lock)
{
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That is correct. You’ll get a
NullPointerExceptionif you try to synchronize usingnull.Not exactly. The lock state is not a property in the normal sense because there is no way you can interrogate it. But yes, the state is part of the object.
(But think about it. How could could the lock state possibly be part of the reference? If it was, how would you be able to synchronize on
thisor an object reference passed as a parameter? References are passed as copies … and a change to one copy can’t propagate to the others. And if the lock state can’t propagate, then two threads with their own copies of the reference can’t synchronize in any meaningful way.)