This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Here in the above statements XYZ.class belongs to which class type...Is it valid to give the above code construct and give an example for above scenario.Need help ranchers
Thanks and Regards
Deepak Lal
When The Going Gets Tougher,The Tougher gets Going
instance methods synchronize on the "this" reference. Which means the Object through which the method is called. A static method doesn't have a "this" reference, because they don't belong to an instance. Static methods synchronize on their class literal, which, in XYZ's case, is XYZ.class. If you want to synchronize something with a static method that is not another static method in the same class, you have to synchronize a code block on the class literal to which that static method belongs.
So in this case, the instance method instanceMethod() will be synchronized with the static method staticMethod(), where, if you just synchronized instanceMethod on the "this" reference, they wouldn't be synchronous.
There are two types of Locks in java, one is object lock and other is class lock. For the object lock, you've to synchronized with the object name or this (this mean the object, which is invoking the synchronized method). And for the class lock, you've to synchronized as your original code.
And, there, XYZ is the class name, which you supposed to lock. If you synchronized on objects, no other method belongs to that object can't be invoked with the different thread. If you synchronized with the class lock, no other threads can invoked any static method on the class.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
David O'Meara wrote:XYZ.class is of type Class. Yes it is valid.
To expand, XYZ.class is the same object that is returned by (new XYZ()).getClass(). As said before, because there is no "this" in static methods, it uses the class version of "this" -- the Class literal.
Abimaran Kugathasan wrote:
There are two types of Locks in java, one is object lock and other is class lock. For the object lock, you've to synchronized with the object name or this (this mean the object, which is invoking the synchronized method). And for the class lock, you've to synchronized as your original code.
And, there, XYZ is the class name, which you supposed to lock. If you synchronized on objects, no other method belongs to that object can't be invoked with the different thread. If you synchronized with the class lock, no other threads can invoked any static method on the class.
1> Can you give me an example for this so that i can clearly understand this...
Rob has also explained but i wanted to know what has "this" to do with "static methods" ? could you please clarify with an example.so that it can be more clear.
Deepak Lal wrote:
Rob has also explained but i wanted to know what has "this" to do with "static methods" ? could you please clarify with an example.so that it can be more clear.
this keyword can't be applied in static context. What is your question?
Abimaran Kugathasan wrote:There are two types of Locks in java, one is object lock and other is class lock. For the object lock, you've to synchronized with the object name or this (this mean the object, which is invoking the synchronized method). And for the class lock, you've to synchronized as your original code.
And, there, XYZ is the class name, which you supposed to lock. If you synchronized on objects, no other method belongs to that object can't be invoked with the different thread. If you synchronized with the class lock, no other threads can invoked any static method on the class.
Can you give me a simple working example please for above scenario. ???