Formatting the main() Method
When we want our code to actually run, We have to get the ball rolling with a main() method. The following rules apply to the main() method :It should be declared public (for the purposes of the exam, assume it must be public).
- It must be marked static.
- It must have a void return type.
- It must have a single String array argument.
- We can name the argument anything we want.
other methods is that it has the signature the JVM is looking for when you invoke Java as follows :
java MyClass
method the one with a signature matching what the JVM is searching for. If it finds the matching method, We are good to go. If it doesn\'t, We get a runtime error like this :
Exception in thread \"main\" java.lang.NoSuchMethodError: main
The tricky thing about this error is that we can get it even when there is a main()
method. The following code compiles fine, but still produces the previous NoSuchMethodError when we try to invoke this class from the command line:class MyClass {
public void main (String [] args) { }
}