Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.