If the compiler reads left to right, top to bottom, and every
application needs to have a main method, why is it OK to put the main
method near the end of the program instead of immediately following
the class?
Bob
Christophe Vanfleteren - 15 Apr 2004 11:36 GMT
> If the compiler reads left to right, top to bottom, and every
> application needs to have a main method, why is it OK to put the main
> method near the end of the program instead of immediately following
> the class?
>
> Bob
What makes you think that? That's not how most compilers work. They build an
abstract source tree (AST) which is some internal representation of your
source code. Order in the source code makes no difference for most of these
things.
And wether or not a class has a main method has nothing the do with the
compiler. The compiler doesn't care about that, main is just another
method.
It is the Java runtime (java.exe) that looks for the main method of the
class you tell it to execute.

Signature
Kind regards,
Christophe Vanfleteren
Bryce (Work) - 15 Apr 2004 17:25 GMT
>If the compiler reads left to right, top to bottom, and every
>application needs to have a main method, why is it OK to put the main
>method near the end of the program instead of immediately following
>the class?
Order makes a difference in Delphi... Not Java... Try writing a
function that references a variable, and declare the variable below
the function...
--
now with more cowbell
Roedy Green - 15 Apr 2004 22:33 GMT
>If the compiler reads left to right, top to bottom, and every
>application needs to have a main method, why is it OK to put the main
>method near the end of the program instead of immediately following
>the class?
Because Java is compiled in multiple passes, once to discover the
symbols and discover their types, then another to fix their locations,
then another to resolve the precise offsets for references. (YMMV).
In Wirthian languages, he wanted one-pass compilation, so forward
references were a problem. They are not in Java.
You can even do forward references in a primitive one pass compiler,
like Forth, by making notes to yourself about forward references that
need to be patched later as soon as they are known.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.