• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait method  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am seeing in code samples that sometimes wait() is called on an object. Sometimes it is called directly as wait() without any object. When should a method be called on an object and when can it be called just like that?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The wait() method of the Object class is an instance method. Hence, it is *always* called using an instance. If you don't see an instance in the code, then it is likely in an instance method, and hence, the instance is referred to by the this reference.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7961
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In modern code, you should never call wait(). Instead, you should use Java's high level concurrency framework. The modern equivalent to Object.wait() is Condition.await().

However, when you're dealing with old code and you're using synchronized blocks, you should call wait() on the object that's being synchronized on. Since you should never synchronize on this, but instead on a private field, your question becomes moot.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't see an instance in the code, then it is likely in an instance method, and hence, the instance is referred to by the this reference.

No there is no "this" keyword used. It is just called as wait() in the try block
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:No there is no "this" keyword used. It is just called as wait() in the try block


For instance methods, the "this" keyword does *not* have to be present -- with no instance, it is implied.

Henry
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote: If you don't see an instance in the code, then it is likely in an instance method, and hence, the instance is referred to by the this reference.

No there is no "this" keyword used. It is just called as wait() in the try block
The "this" is implied in that case, just as it is when you call any other instance method without an explicit reference. That means that wait() is called on the current object.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:No there is no "this" keyword used. It is just called as wait() in the try block

compiler will do a job then if it is not applied.

it is not just for wait() only but for every instance method example-


Hope this will help!

kind regards!
Praveen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!