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!

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


Carlos Caro
Carlos Caro
4,174 Points

If there is more than one class, and each class has a method called "Main". What method is executed first?

If the program has more than one class and each class has a method called "Main". Whats executed first? which class and which method?

1 Answer

Since the Main method is intended to be your "main" entry-point into the application, there can only one can be defined as your program's entry point. You can define multiple main methods and adjust the compiler settings to define the entry point or use command-line switches to choose one, but essentially it's still only using one "main" to get the job done. Defining more than one main is sometimes helpful for testing and different platform compilations, but you are probably better off by defining one main and branching out from there unless you're doing some fairly fancy stuff that requires quick changes of entry points to test things.