This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wait method  RSS feed

 
Ranch Hand
Posts: 197
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?
 
author
Marshal
Posts: 23438
138
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
 
Saloon Keeper
Posts: 8617
161
  • 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: 197
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
Marshal
Posts: 23438
138
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
 
Sheriff
Posts: 11747
191
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.
 
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!