Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main Method

 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah!! its much more clear now thanks for your replies guys
 
Aj Maven
Greenhorn
Posts: 22
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)"
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic