.floating { position: fixed; bottom: 0; border: 2px solid green; left: 300px; background-color: lightblue; } freshers jobs.Durgajobs.com (IT Jobs, Government Jobs, Bank Jobs)





Untitled Document

Subscribe here to get updates on new Videos

 

11. Explain about public static void main(String[] args); ( PART- I) ?

 

 

 

previous homenext

 

Notes:

Whether class contains main() method or not and whether main() method is declared according to requirement or not this things wont be checked by compiler. at runtime JVM is responsible to check this things.

At runtime if JVM is unable to find required main() method then we will get runtime exception saying NoSuchMethodError:main

class Test
{
}

Javac Test.java

java Test

RE: NoSuchMethodError:main

The above syntax is very strict if we perform any change we will get runtime exception saying NoSuchMethodError:main.

Even though the above syntax is very strict the following changes are acceptable.

1. The order of modifiers is not important that is instead of “public static” we can take “static public” also.

2. We can declare “String[]” in any acceptable form.

main(String[] args)
main(String []args)
main(String args[])

3. instead of ‘args’ we can take any valid java identifier.

4. we can replace String[] with var arg perameter

main(String[] args) ==> main(String… args)

5. We can declare main() method with the following modifiers also.

final, synchronized, strictfp

class Test
{
final static synchronized strictfp public void main(string… durga)
{
sopen(“void main”);
}
}

javac Test.java

java Test

output: void main

which of the following are valid main method declarations?

1. public static void main(String[] args)

2. public static int main(String[] args)

3. public static void main(String args)

4. public final synchronized strictfp void main(String[] args)

5. public static final synchronized strictfp void main(String[] args)

6. public static void main(String[] args)

Q2) In which of the above cases we will get compile time error?

A) we wont get compile time error any where but at runtime we will get exception in all cases except last two.