Sunday, October 20, 2019
Using Multiple Main Classes in Java
Using Multiple Main Classes in Java Normally at the outset of learning the Java programming language, there will be a number of code examples that are useful to compile and run to fully understand them. When using an IDE like NetBeans its easy to fall into the trap of creating a new project every time for each new piece of code. However, it can all happen in one project. Creating a Code Example Project A NetBeans project contains the classes needed to build a Java application. The application uses the main class as the starting point for the execution of the Java code. In fact, in a new Java application project created by NetBeans only one class included - the main class contained within the Main.java file. Go ahead and make a new project in NetBeans and called it CodeExamples. Lets say I want to try programming some Java code to output the result of adding 2 2. Put the following code into the main method: public static void main(String[] args) {int result 2 2;System.out.println(result);} When the application is compiled and executed the output printed is 4. Now, if I want to try out another piece of Java code I have two choices, I can either overwrite the code in the main class or I can put it in another main class. Multiple Main Classes NetBeans projects can have more than one main class and its easy to specify the main class an application should run. This allows a programmer to switch between any number of main classes within the same application. Only the code in one of the main classes will be executed, effectively making each class independent of each other. Note: This is not usual in a standard Java application. All it needs is one main class as a starting point for the execution of the code. Remember this is a tip for running multiple code examples within one project. Lets add a new main class to the CodeSnippets project. From the File menu choose New File. In the New File wizard pick the Java Main Class file type (its in the Java category). Click Next. Name the file example1 and click Finish. In the example1 class add the following code to the main method: public static void main(String[] args) {System.out.println(Four);} Now, compile and run the application. The output will still be 4. This is because the project is still set up to use the Main class as its main class. To change the main class being used, go to the File menu and choose Project Properties. This dialog gives all the options that can be changed in a NetBeans project. Click on the Run category. On this page, there is a Main-Class option. Currently, it is set to codeexamples.Main (i.e., the Main.java class). By clicking the Browse button to the right, a pop-up window will appear with all the main classes that are in the CodeExamples project. Choose codeexamples.example1 and click Select Main Class. Click OK on the Project Properties dialog. Compile and run the application again. The output will now be four because the main class being used is now example1.java. Using this approach its easy to try out lots of different Java code examples and keep them all in one NetBeans project. but still be able to compile and run them independent of each other.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.