Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Karel run without "main" method?

Tags:

java

karel

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?

like image 968
user3276091 Avatar asked Mar 17 '26 15:03

user3276091


2 Answers

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.

like image 94
nickgroenke Avatar answered Mar 19 '26 04:03

nickgroenke


a "main" method is the starting point of every java program. What is going on with this class is that its not a java program by itself, is executed inside some type of framework (karel robot java implementation in this case), this frameworks off course have a "main" method but not this class, the framework knows how to load this class and execute his run method.

This "programs" are frameworks designed to do a specialized type of programs, i don't know this "karel framework", but for example, when you program a java web application you write a "servlet" but you don't write a "main" method. There are some programs called "application servers" that off course have a "main" method and take this servlet class and execute for response to some http message.

like image 29
AlfredoCasado Avatar answered Mar 19 '26 03:03

AlfredoCasado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!