5

In my text book they put the main function at the bottom of the code examples. As a conventional book reader, I start reading from line 1. While I do understand that coding has a different control flow than there is in books (from bottom to top, line per line), I do not understand why you would want your first line of code not to be the entry point of your program. Is there particular reason for this?

Joop
  • 3,706
  • 34
  • 55
  • 1
    I think it is just the writer's coding style. The only thing that comes to my mind is that the writer could do this to reflect how things work in the stack, where the main is the first element (so the one at the very bottom) – BackSlash Nov 17 '14 at 09:03
  • 1
    I guess that dates back to old C times, where it was crucial what you declared first. – Fildor Nov 17 '14 at 09:06
  • possible duplicate of [Preferable location of main() method in Java class file](http://stackoverflow.com/questions/7911829/preferable-location-of-main-method-in-java-class-file) – Andreas Fester Nov 17 '14 at 09:07
  • Some inexperienced programmers (or rather, programmers not experienced in working in large codebases) think that its easier to read the entire file if you've already read the definitions of everything you'll come across lower down. They think this saves them from properly documenting their functions and makes it easier to read. They're wrong however, its substantially harder to read because programming is about abstraction and hiding information, not about reading all the details of everything. Don't put hide your entrypoint folks. – B T Dec 21 '22 at 22:45

2 Answers2

8

I can't speak for the author but I can think of several reasons:

  1. The author may have wanted to emphasize the actual methods in the class. In practice very few Java classes even have main methods (less than 1%). What is important in a class is how its objects behave; perhaps the main method is almost an afterthought or nice-to-have.

  2. Java was influenced by the C programming language. Although in Java the order in which methods are declared does not matter, in C (at least in older versions of C), you could only call functions from main if they were declared above main. So people either used prototypes or just put main at the bottom. So many people did this out of habit that, oh I don't know, this subconsciously influenced their thinking that they carried this over into Java.

But your sense that the entry point should appear first, and that functions should call each other going "down the page" is shared by many people. Robert C. Martin (Uncle Bob) has said this is a Good Thing.

ADDENDUM

I was looking for a reference to Uncle Bob's suggestion of reading down the page, something known more-or-less as the Newspaper Metaphor. I found this snippet from a book review of Martin's Clean Code book:

... the newspaper metaphor that is mentioned with regards to formatting your code. This describes the idea of making code read like a newspaper article. We should be able to get a general idea of how it works near the top of the class before reading more and more details further down.

But, you know, I really think it is a tradition inherited from C (gotta declare those called functions first) or a perceived emphasis on bottom-up-ness that makes main often appear on the bottom in Java.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • The reference to how it should be in c makes it easy for me to see why people would do it in java as a consistency thing. Thanks! – Joop Nov 17 '14 at 09:17
2

The main function is the entry point. here is no hard and fast rule that main has to appear at the bottom part. It's entirely prerogative of the developer. One logic is main function builds upon several other functions and data. The declaration of those dependent functions need to be declared and defined in Java as well so that main function is able to access them (in a way forward declaration is avoided).

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • Why do you _want_ to "avoid a forward declaration"? Just curious. – Ray Toal Nov 17 '14 at 09:12
  • @RayToal For a language like C++, if a function f that is called within a function, say, g, then if f is defined prior to g, separate declaration is not needed, otherwise, f needs to be declared first (forward reference) and defined later, two places you need to have f, declaration and definition. In Java, definition includes declaration, so we need to have function reference in one place. That way. – Dr. Debasish Jana Nov 17 '14 at 09:16