• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

main method and strings

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen when you compile and run the following program?
1 ublic class Q11
2:{
3: static String str1 = "mainmethod with String[] args";
4: static String str2 = "mainmethod with int[] args";
5:
6: public static void main(String[] args)
7: {
8: System.out.println(str1);
9: }
10: public static void main(int[] args)
11: {
12: System.out.println(str2);
13: }
14:}
A) Duplicate method main(), compilation error at line 6
B) Duplicate method main(), compilation error at line 10.
c) prints "main method with main String[] args"
d) prints "main method with int[] args";
I thought the answers are c and d. But when i ran the program the answer was only c. Why is it...? Can anybody help me through this...?


 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main method can be overloaded or overrriden !
simple !
The entry point for the JVM is the method which takes String[] as the argument !
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main() method is just like any other static method.
It can be overloaded. See the signature of the second main()is different. JVM knows only one main(), that with String[] as its argument.So to execute a class, JVM calls main() with String[] argument.If it is there ,fine. If one more is there with different signature, does not care.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would like to contradict with you Deepak regarding the point
that main method being overriden.I don't think they can be overriden in a file there can be only one correctly formed
main method.
Any remarks ??
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just give it a try -- u'll find that main can be both overloaded & overriden.
- Shankar.

Originally posted by Karthik Subramanian:

I would like to contradict with you Deepak regarding the point
that main method being overriden.I don't think they can be overriden in a file there can be only one correctly formed
main method.
Any remarks ??


 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shankar,

I just read your reply and because the m/c i am working
on does not have java loaded int it, i am unable to try
it now.But i will try later.
Before that i would like to make a point here thatwhenever we override a method in a subclass(obviously!!) we need to provide the same declaration as the original method of course with the return type being not more private.
Let us not forget that correctly formed main method is
a static method and i am sure that you will agree that
static methods cannot be overriden.
In that context we can have only one correctly formed
main method per source file.
Any further constructive criticism will be warmly accepted.
Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam having a doubt if we give Object Array as aruguments instead of string the Jvm at run time throws error NO main method.
Why the cannot we able to give only for the main method.
We can use parent type references to hold the chile type object.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether the main method can be overloaded is a different issue and so is object-reference type conversions. When you try to run a .class file, the JVM always looks for the correctly formed main method. If no such method is found, it will throw an exception.
It just happens to be a rule of the game you have to follow when you play...
Ajith
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic