0

I'm just learning Java and I have a problem understanding what the run() method does. I decided to ask this question after 3 days of trying to get it myself, and it's already frustrating. Is the run() method built-in or user defined? In one of the tutorials that I'm reading it says that the run() method has the followind instructions:

move();
pickObject();
move();

and then the tutorial says that I can define other methods as I need them so as to not write again and again another long piece of code, but then when it uses the run() method it still has those 3 lines of instructions, and sometimes other lines, so: is it a stand alone method or not? Sorry if the question seems complicated but it's so frustrating.

EDIT: after reading the answers that have come until now (thank you all for that) I must say that (as I am beginner, as in: I'm just learning what a method is) I have no idea what a Thread is or Runnable or any of the terms presented.

public void run() {
move();        \
pickBeeper();  = if these are the commands for the run() method then why are they being written
move();        / again here? isn't that the point of a method as not to write the code again?
turnLeft();      
move();
turnLeft();
turnLeft();
turnLeft();
move();
move();
putBeeper();
move();
}

shouldn't it be:

public void run() {
run();
turnLeft();
move();
turnLeft();
turnLeft();
turnLeft();
move();
move();
putObject();
move();
}

Also, can the run() method be inside itself?, and shouldn't the rest of the code be put separately? between other brackets or something? And, can the run() method have 2 instances with different commands? Like the first run() has the 3 commands and then it has the same 3 and some more...

4 Answers4

0

Probably the run() method is defined as a kind of "execute" shortcut.

That is, instead of writing move(), pickup(), move() we just write run() every time we want that to happen.

run() in this case is a user defined method, and could as well be called runProgram(), execute(), or poop(). It's just a shortened form of move, pickup, move.

EDIT: Never mind, it appears due to the above that Runnable is an interface. In this case, most of what I said still applies: you must still define the run() method to satisfy the fact that you are implementing a Runnable interface. It's just that you must actually call it run(), not whatever you want.

djbhindi
  • 364
  • 3
  • 13
0

The run() method is indeed found in the Thread class and is used to execute it. An example of run() in a Thread can look something like this:

public void run() {
    if (target != null) {
        target.run();
    }
}

The run() method would be in the stackframe if the user overrode it in a Thread superclass. It could be removed by the JDK to improve stackframe output.

0

move() and pickObject() are the methods that you want to use in this case. They both could contain a lot of code so you do not want to write each one out every time when you could just call move() in one line, for example. run() is just a method that you can use to call, in your case, two move()'s and a pickObject(). When you call run later it will execute a move(), pickObject(), and move() in that order.

I should also mention that run() is usually called when you want it to execute on a separate thread.In terms of threads, run() just defines what should happen on the other thread.

user3799575
  • 217
  • 2
  • 7
  • so then where should the method be? in the header or the body? also if the method is in the header does it have to be in the body as well? and if it is either way: doesn't it have to be described somewhere in the code so that Eclipse knows that run() means those 3 lines of code? – user3817396 Jul 08 '14 at 18:05
  • The method header "public void run()" just lets the program know that there is a method called run() that returns void. Then the body of the method is what tells the program what to do when run() is called. You don't need to call run() inside the run() method. You should call run in a different part of the program, for instance, in a separate method or in the main method. – user3799575 Jul 08 '14 at 18:17
  • so in the body of the run() method what I am doing is to define the method? then run could have any other name - I tried this and it didn't work. also: when other lines of code are added after the first three is it the same method or something else? and if after this I invoke the method will it execute the first 3 lines of code or all of them? – user3817396 Jul 08 '14 at 18:27
  • Anything that goes inside the { } of the run method is considered part of the method and will be executed when you call run(). It is basically instructions of what to do when run() is called. If you change the name of the method, say from run() to go(), you would have to call go() later in the program as run() would no longer apply to that method. – user3799575 Jul 08 '14 at 18:37
  • thanks - it's not completely clear but I think you've made some light into the situation... – user3817396 Jul 08 '14 at 19:36
0

The run method should be implemented by a class that implements the Runnable interface. The run method isn't normally called directly - instead the start method is used to start a thread and begin execution of the thread..

EXAMPLE FROM URL: http://www.tutorialspoint.com/java/lang/thread_run.htm

package com.tutorialspoint;

import java.lang.*;

public class ThreadDemo implements Runnable {

   Thread t;

   ThreadDemo() {

      // thread created
      t = new Thread(this, "Admin Thread");

      // prints thread created
      System.out.println("thread  = " + t);
      // this will call run() function
      t.start();
   }

   public void run() {
      System.out.println("Inside run()function");
   }

See these URL's:

When would you call java's thread.run() instead of thread.start()?

What's the difference between Thread start() and Runnable run()

Community
  • 1
  • 1
A B
  • 4,068
  • 1
  • 20
  • 23