my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main method declaration doubt! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main method declaration doubt!" Watch "main method declaration doubt!" New topic
Author

main method declaration doubt!

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Why can't I declare main as


NetBeans says:



I know it works only when either it is written as



OR as



But why? Suppose I do not want to pass any arguments? Why shouldn't the ARGUEMENT-LESS main() work?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

If you do not pass arguments the args variable will be null. No problems


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
but why cant I declare it like this:



What is wrong with this?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

The JVM does not recognize that signature as the main method that it should trigger. The main method always takes a String... args argument
vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
Akash
static public void main(){ }


jvm reconizes the main method with the syntax
public static void main(String[] args)

if you don`t pass any argument in that main method jvm will
not recognize that main method and can`t start execution.

you can declare main method as:



scjp 6
http://algorithmsea.blogspot.com
Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
Aakash Goel wrote:but why cant I declare it like this:



What is wrong with this?


Nothing.

This is a valid signature for a method.

However, to run your program you will need a main method with the (String[] args) parameter as this is the specific method signature that the JVM looks for when running your program. This is how Java itself is written, you can't change it.

You can call your main() method from your main(String[] args) method (if you really want to).
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Thanks everyone. Got the point.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I wanted to add that if you don't pass any arguments to main, args won't become null. Instead, it will have zero elements.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method declaration doubt!
 
Similar Threads
Error in Object Creation?
var args method
Tell me Why ?
overloading
Polymorphism and casting