• 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

this.yield ??

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please confirm my answer to the following question:
What is the result of compiling and runing the following code?

I say that you cannot use "this" with yield, since yield is a static method and therefore this code will not compile at LINE 1.

Can anybody concurr?
thank you
g
[ September 24, 2007: Message edited by: Gary Marshall ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we can't confirm you answer unless you tell us what your answer is. Have you tried compiling and running this to see what happens? that's probably going to be faster than asking someone here.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred is right, try it by yourself. Anyways I ran it & I got compiler error at line 1 "cannot fine symbol method yield()".
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I say that you cannot use "this" with yield, since yield is a static method and therefore this code will not compile at LINE 1.


Its ok to use this on static members.
Remember where yield() is defined so you can fix your program.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Its ok to use this on static members.



Can somebody show me with an example how this can be used on static members. Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your class extends Thread, rather than implementing Runnable, then the code will be running fine (if you call a static method on an instance of the class, the compiler will replace that line of code with an actual call to the class instead, i.e. this.yield() becomes Thread.yield())

class MyThread extends Thread {
public void run() {
System.out.print("go ");
this.yield(); //LINE 1
System.out.print("gogo");
}

public static void main(String [] args) {
(new MyThread()).run();
}
}
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwardo,
whats the difference between the two? why it does not work if class is implementing. Both means the same in both cases we are overriding run method and reference is class instance. Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
Hi Edwardo,
whats the difference between the two? why it does not work if class is implementing. Both means the same in both cases we are overriding run method and reference is class instance. Thanks.



In the first case, the class is a Runnable. In the second case, the class is both a Thread object and a Runnable. Since the yield() method is a method of the Thread class, being a Runnable object is not good enough -- you have to be a Thread object to have the yield() method.

Henry
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. Makes perfect sense!!
 
Eduardo Mendes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sridhar, I am just not sure you have the answer to your original question. If the question was put to you as you have transcribed it here (with implements Runnable rather then extends Thread) than it is wrong, and this.yield() would never even compile, for the reasons mentioned in the previous two posts.
reply
    Bookmark Topic Watch Topic
  • New Topic