Search code examples
javakarel

How does Karel run without "main" method?


I was wondering in the program like Karel the Robot runs because it does not used main() method. Instead it used run() method:

import stanford.karel.Karel;

public class CollectNewspaperKarel extends Karel { 
  public void run() { 
    move(); 
  } 
}

How does it work?


Solution

  • The actual main method is somewhere else. For example, in the KarelRunner class. When java executes the program, it's actually executing the main method in the runner class. Your own run method is called from that runner code.