javablogspot

Just another WordPress.com weblog

Correct declaration for a main() method

Posted by damuchinni on March 11, 2009

Correct declaration for a main() method

In the Java language, main() method allows us to run the class as an application. When we execute a class with the Java interpreter, the runtime system starts by calling the class’s main() method. The main() method then calls all the other methods required to run our application.

The main() method must be static, not return a value, and take an array of strings.

Signature:

public static void main(String[] args) or
public static void main(String args[])

Here public indicates that the main() method can be called by any object.static indicates that the main() method is a class method and void indicates that the main() method has no return value.

main() method can be overloaded

We can create any number of methods called ‘main’ in a class, which take other arguments, are not public and static, or return a value. To be able to run the class, however, there must be one method called main that takes an array of Strings as an argument, and that method must be static, and not return a value. Otherwise we will get a runtime error when you try to execute the class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: