• Post Reply Bookmark Topic Watch Topic
  • New Topic

main method  RSS feed

 
manogna edintipal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a main method overloaded or overrided? If yes can U show me an example.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It cannot be overridden because it is static.

It can be overloaded: public static int main (int[][] junk). That is an overloaded main method - but don't expect it to be called when the program starts.

Also main can be hidden by a subclass of a class which has a declaration of main with the normal signature.
 
manogna edintipal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls send me one simple example.
Thanks in advance
 
Eideo Guo
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but there are two main methods in the class,which method could be invoke by JVM.
[ August 26, 2005: Message edited by: Eideo Guo ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


It cannot be overridden because it is static.
.....

Also main can be hidden by a subclass of a class which has a declaration of main with the normal signature.









Yes you are right about overloading....buw overriding..can you be clear about that..?

I over rode the main(String[]) in the sublcass....and the
parent main(String[]) was visible in the sublass....

Regards
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eideo Guo:
but there are two main methods in the class,which method could be invoke by JVM.

[ August 26, 2005: Message edited by: Eideo Guo ]


The one that matches the signature

public static void main(String args[])

J
[ August 26, 2005: Message edited by: Joanne Neal ]
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code is posted in the above thread. While running it is giving some error.

Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at MainOverload.main(MainOverload.java:8)

And also I think since the main method is static we can't override it, but overhide it. Am I correct.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is expecting an argument to be passed to it. Are you passing one ?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was passing a command line argument....
Removed that part....now u canpaste nad xcute it..




And also I think since the main method is static we can't override it, but overhide it. Am I correct.



What do u mean by overhide??Sorry didnt get it


Regards
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Normally if we try to override the static methods, we are really hiding the method in the main.

However I am not sure about it.

May be this web pagelink will be useful. (8.4.6 section)
[ August 26, 2005: Message edited by: Mark Henryson ]
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

One small doubt, may be a silly doubt:

Whether the overloaded main method should have an array as an argument.
i.e., public static void main(int[] args)
{
...

Whether we can have like this as below:
i.e., public static void main(int i)
{
...

Is there any rules to have only array type of arguments to the main method(Overloaded main method)
[ August 26, 2005: Message edited by: Mark Henryson ]
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Sorry, I should have done some home work, before posting this quesion.

Yes. It is working, without the array argument to the overloaded main method.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mark,


u can sure have any type of argument....for the main..



Regards
 
phil shea
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really not getting what is the exact question of this particular thread.

Can the main method be overloaded or overridden?

I don't really understand what anyone can achieve by overriding the main method.

We can have any number of methods with the name "main" as long as their signatures are different. But "java" is only concerned with the main with the following signature.
public static void main(String args[])
This method you can have only one in a single class. And that is the method that "java" calls when a program is executed as an application.

-phil
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!