• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clarification in Class Type's in Java

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XYZ.class is of type Class. Yes it is valid.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any updates on this post...abimaran could you provide me wtih an example.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic