aspose file tools*
The moose likes Beginning Java and the fly likes Main Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main Method" Watch "Main Method" New topic
Author

Main Method

jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

jaspreet manchinda wrote:Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it

1. A public, static and void method named main which takes no arguments.
2. A public, static and void method named main which takes argument as array of String. This method also acts as entry point of program (if main class is proper et-cetera)
3. Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
Quote:
Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.

thank you for your reply but when to use main without argument and when to use wit var-args
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

jaspreet manchinda wrote:Quote:
Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.

thank you for your reply but when to use main without argument and when to use wit var-args


Your java program execution always begin with standard main method with standard arguments i.e. public static void main(String[] args){}.
But you can at any no. of times overload main method in your same program as per your requirements.

I hope it is clear to you now.


Vinod Vijay Nair
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
yeah!! its much more clear now thanks for your replies guys
Aj Maven
Greenhorn

Joined: Jul 04, 2011
Posts: 22

jaspreet manchinda wrote:Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it



1. Is a normal method which is being used in the program and is not the point of entry of execution of the program, having this method. Its just a static method.
2. This is the method which is the point of entry for execution of a program. Execution will start from here.
3. String args[] holds the same meaning as String ... args, where the later is called var-ags which takes or holds multiple values of type String. All that is said in '2', applies here too.

Thanks
-Aj
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055



At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime


http://www.lifesbizzare.blogspot.com || OCJP:81%
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime


Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Vinod Vijay wrote:
Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime


Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().


Thats what I was conveying
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

Vishal Hegde wrote:

Note that the above is no different than

The larger point here is that main() is no different from any other method. You can overload it, you have have it or even not have it. It is all valid java. The only time it matters is when you want to START your program. The JVM looks for the main method with the signature of "public static void main(String [] args)"


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

Vishal Hegde wrote:
Vinod Vijay wrote:
Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime


Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().


Thats what I was conveying


I know, just added my words to that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main Method