Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Could someone explains javas main function to me?

"The code in the main method executes first when the program starts, and is the control point from which the controller class accessor methods are called to work on the data." I'm a bit confused my this, in JavaScript programs simply read from bottom down. Does this mean in Java the program starts were ever the main function is and jumps around?

1 Answer

Hello

In Strongly Typed Languages such as Java and C#, an application must have an Entry Point. Strongly Typed means that unlike Javascript, you must define the data type of each variable explicitly.

Back to the question.

if you recall, main is a public static void main(..........) { ,,,,,,,,,,, }

Using main, you "Enter" the application ... meaning you start with main and call other functions that call other functions and in turn calls other functions ... and so on.

These function are Stacked in "Memory Stack" .... think of Stack as bunch of boxes on top of each other ... function main at the bottom (first one), the first function you call off main is next ... and so on.

Now think about .... when the function are done doing what they supposed to be doing, they exit, and one box get offloaded off the stack.

So eventually all functions are done (all boxes are removed of the stack .... and you down back to the first box ... main.

But main is

public static void main(..........) { ,,,,,,,,,,, }

it return nothing ... so when you exit main .... you are don done done ... no boxes left in the stack.

Hope this help

one more thing ... main is a static method ... there can only be one main ... hence you enter it once, and you exit it once.