This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Clarification in Class Type's in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clarification in Class Type Watch "Clarification in Class Type New topic
Author

Clarification in Class Type's in Java

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi,
i have below piece of code in java


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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

XYZ.class is of type Class. Yes it is valid.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4



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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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%|
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi ranchers,
Thanks for your impeccable replies.

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.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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. ???

Hi any updates on this.....
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

any updates on this post...abimaran could you provide me wtih an example.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification in Class Type's in Java