Friday, November 8, 2019
A Main Class in Java Contains the Main Method
A Main Class in Java Contains the Main Method All Java programs must have an entry point, which is always the main() method. Whenever the program is called, it automatically executes the main() method first. The main() method can appear in any class that is part of an application, but if the application is a complex containing multiple files, it is common to create a separate class just for main().à The main class can have any name, although typically it will just be called Main. What Does the Main Method Do? The main() method is the key to making a Java program executable. Here is the basic syntax for a main() method: public class MyMainClass { public static void main(String[] args) { // do something here... }} Note that the main() method is defined within curly braces and is declared with three keywords: public, static and void : public: This method is public and therefore available to anyone.static: This method can be run without having to create an instance of the class MyClass.void: This method does not return anything.(String[] args): This method takes a String argument. Note that the argument args can be anythingà - its common to use args but we could instead call it stringArray. Now lets add some code to the main() method so that it does something: public class MyMainClass { public static void main(String[] args) { System.out.println(Hello World!); }} This is the traditional Hello World! program, as simple as it gets. This main() method simply prints the words Hello World! In a real program, however, the main() method just starts the action and does not actually perform it. Generally, the main() method parsesà any command line arguments, does some setup or checking, and then initializes one or more objects that continue the work of the program.à Separate Class or Not? As the entry point into a program, the main() method has an important place, but programmers do not all agree on what it should contain and to what degree it should be integrated with other functionality. Some argue that the main() method should appear where it intuitively belongs - somewhere at the top of your program.à For example, this design incorporates main() directly into the class that creates a server: However, some programmers point out that putting the main() method into its own class can help make the Java components you are creating reusable. For example, the design below creates a separate class for the main() method, thus allowing the class ServerFoo to be called by other programs or methods: Elements of the Main Method Wherever you place the main() method, it should contain certain elements since it is the entry point to your program. These might include a check for any preconditions for running your program. For example, if your program interacts with a database, the main() method might be the logical place to test basic database connectivity before moving on to other functionality. Or if authentication is required, you would probably put the login information in main(). Ultimately, the design and location of main() are completely subjective. Practice and experience will help you determine where best to put main(), depending on the requirements of your program.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.