Lỗi class does not have a main method năm 2024
The Java
Show 7 method is usually the first method you learn about when you start programming in Java because its the entry point for executing a Java program. The
7 method can contain code to execute or call other methods, and it can be placed in any class that’s part of a program. More complex programs usually have a class that contains only the
7 method. The class that contains the
7 method can have any name, although typically you can just call the class
1. In the examples that follow, the class that contains the
7 method is called
3: Test.java
In this article you’ll learn what each component of the
7 method means. The syntax of the
7 method is always:
You can change only the name of the
6 array argument. For example, you can change
7 to
8. The
6 array argument can be written as
0 or
1. The access modifier of the
7 method needs to be
2 so that the JRE can access and execute this method. If a method isn’t public, then access is restricted. In the following example code, the
7 method is missing the
2 access modifier: Test.java
When you compile and run the program, the following error occurs because the
7 method isn’t public and the JRE can’t find it:
When the Java program starts, there is no object of the class present. The
7 method has to be
8 so that the JVM can load the class into memory and call the
7 method without creating an instance of the class first. In the following example code, the
7 method is missing the
8 modifier: Test.java
When you compile and run the program, the following error occurs because the
7 method isn’t
8:
Every Java method must provide the return type. The Java
7 method return type is
6 because it doesn’t return anything. When the
7 method is finished executing, the Java program terminates, so there is no need for a returned object. In the following example code, the
7 method attempts to return something when the return type is
6: Test.java
When you compile the program, the following error occurs because Java doesn’t expect a return value when the return type is
6:
0 The Java
7 method is always named
7. When a Java program starts, it always looks for the
7 method. The following example code shows a
7 method renamed to
8: Test.java
1 When you compile and run the program, the following error occurs because the JRE can’t find the
7 method in the class:
Java
7 method accepts a single argument of type
6 array. Each string in the array is a command line argument. You can use command line arguments to affect the operation of the program, or to pass information to the program, at runtime. The following example code shows how to print the command line arguments that you enter when you run the program: Test.java
4 When you compile the program and then run it with a few command line arguments separated by spaces, the arguments get printed in the terminal:
5
6 In this article you learned about each component of the Java
7 method. Continue your learning with more Java tutorials. |