• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification in Class Type's in Java

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XYZ.class is of type Class. Yes it is valid.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any updates on this post...abimaran could you provide me wtih an example.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic