October 15, 2009

How many ways to declare main method in Java Version 5?

Main method can be created in the below ways:
public static void main(String[] args) {}

public static void main(String molecules[]) {}

static public void main(String[] gadgets) {}

static public void main(String spaces[]) {}

public static void main(String ... countries) {} //String three dots(...) args/any variable name

static public void main(String ... data) {}

1. public and static can appear in any order in main function
2. [] can appear before or after the variable name
3. as of java 5 [] can be replaced with ...(three dots), but it should appear between String and the variable. 9This feature is called var-args and has been added in java 5.0. You can use var-args in any method or constructor. It must be the last arguments. you can pass only one var-arg argument. var-arg argument is considered as a array. so a class cannot have 2 methods one havin g a array of String and another having only var-args of String)
4. the most important is that argument's name can be anything ie instead of args we can name it with our own name.
i think these are the ways main method can be written.

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts