public static void main(String args[])

hi,Can anyone explain what each of the following denotes?public static void main(String args[])thanks
[129 byte] By [Pannara] at [2007-11-27 7:30:03]
# 1
Should all be covered here:[url= http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]You could also try here: http://www.google.com
jverda at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 2
This forum doesn't need respondents, it needs to redirect to Google.Joe
Joe_ha at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 3
Did you not pay attention in class? All this should have been explained. If not then your teacher probably hasn't covered that material yet and you should be patient.
floundera at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 4

i don't know why do we need to declare the main method as public?if we declare it as public, that class can be called even outside of the package?

do we need to declare main method as static, since JVM wont be confusing with similar not-static main method defined in the same class?

Java by default does n't return any value unlike C++,C# , thats why we put it as void?

even though we are not passing any arguments thru command line interface, String array args[] need to be passed as a parameter in main method unlike C++,C#?

Pannara at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 5

The main method is the entry point to your program for the JVM. If you understand what static means then you will understand why the main method has to be static. You can call as many methods main as you like (as long as they had diff parameter lists) and make them static or non-static but I wouldn't.

floundera at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 6

> i don't know why do we need to declare the main

> method as public?

Because the spec says that the VM looks for a public main method to start the program.

> if we declare it as public, that

> class can be called even outside of the package?

Yes. That's what public means. The VM is not part of your package.

> do we need to declare main method as static, since

> JVM wont be confusing with similar not-static main

> method defined in the same class?

Try making it non-static and see what happens.

> Java by default does n't return any value unlike

> C++,C# , thats why we put it as void?

Yes, that's what void means--no value is returned. Your main method does not return a value to the VM.

> even though we are not passing any arguments thru

> command line interface,

Says who? I pass command line args all the time.

> String array args[] need to

> be passed as a parameter in main method unlike

> C++,C#?

The spec says the VM looks for that exact signature. This is so that you can pass command line args, though not all programs need them. Depends on your particular program.

jverda at 2007-7-12 19:10:13 > top of Java-index,Java Essentials,New To Java...
# 7

As for command line arguments:

class Foo {

public static void main(String[] args) {

if(args.length > 0) {

for(int index = 0; index < args.length; index++) {

System.out.println(args[index]);

}

} else {

System.out.println("You did not enter any command line arguments");

}

}

}

floundera at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 8
Excellent answers from Jverd !! Thanks a lot i got cleared my doubts from you people valuable answers and hints!! This was the interview qtn. :-)
Pannara at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 9
> This was the interview qtn. :-)So Jeff got the job? Well congratulations to him then.
cotton.ma at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 10
Oracle tech.people only have to decide whether am eligible to work there or not!
Pannara at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 11
> This was the interview qtn. :-)> Oracle tech.people only have to decide whether am eligible to work there or not!So what was your answer in the interview then?
tschodta at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 12

here was my answer :

1.we should have to declare the method as public, only then we can run this class even outside of package which contains this class.

2.We have to put static in main method , otherwise javan will throw the compile time error since it will be confused with which method can be executed if two main methods(with static without static) are there in the same class.

3. by default java main method doesn't return any value to jvm , thats why its void return type unlike C++,C#.

4. We have to put the exact type and parameter String args, even though we r not passing any args thru command line interface, its standard spec by JVM. :-)

Is it correct or somewhat correct?

Do you people know that where will you invoke the user-defined methods defined inside the Action class outside execute method? this is related with Struts :-)

Pannara at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...
# 13

> 1.we should have to declare the method as public,

> only then we can run this class even outside of

> package which contains this class.

It has nothing to do with running a class, and it just modifies the *method's* (not the class's) visibility to other classes anyway. With main(), that's just a convention - the JVM isn't a class and thus isn't access-restricted in any way. Who would execute the private methods if it were?

But it's just public because the specs say it needs to be. No other reason.

> 2.We have to put static in main method , otherwise

> javan will throw the compile time error since it will

> be confused with which method can be executed if two

> main methods(with static without static) are there in

> the same class.

No. You have to put static there because non-static methods cannot be invoked without having an instance to invoke them on, and without starting the program, you can't create an instance. So you need an entry point at class level (or some mechanism that automatically instantiates the entry class, but that would be a big nuisance).

The compiler won't have a problem with you omitting the "static", but the JVM will have a problem finding an entry point.

> 3. by default java main method doesn't return any

> value to jvm , thats why its void return type unlike

> C++,C#.

Again, it's just because the specs said so. They could have defined it to be int.

> 4. We have to put the exact type and parameter String

> args, even though we r not passing any args thru

> command line interface, its standard spec by JVM. :-)

You're still passing an array with 0 elements. Further, methods are distinguished by name and argument types. So a method main() and a method(String[]) are completely different ones and not interchangeable.

CeciNEstPasUnProgrammeura at 2007-7-12 19:10:14 > top of Java-index,Java Essentials,New To Java...