Formatting the main() Method

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 :
  1. It must be marked static.
  2. It must have a void return type.
  3. It must have a single String array argument.
  4. We can name the argument anything we want.
It should be declared public (for the purposes of the exam, assume it must be public).
                 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) { }
}

By:UMANG
Date:2025-02-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher