• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question form Majji's exam

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 11
What will happen if you compile/run the following code?
1: public class Q11
2: {
3: static String str1 = "main method with String[] args";
4: static String str2 = "main method with int[] args";
5:
6: public static void main(String[] args)
7: {
8: System.out.println(str1);
9: }
10:
11: public static void main(int[] args)
12: {
13: System.out.println(str2);
14: }
15: }
A) Duplicate method main(), compilation error at line 6.
B) Duplicate method main(), compilation error at line 11.
C) Prints "main method with main String[] args".
D) Prints "main method with main int[] args".
The answer is C.
Can somebody explain this. I thought there would be an compile error about having 2 main methods
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, essentially you are overloading the 'real' main
method so the compiler doesn't care. You have a
different parameter going in. main is not really
'reserved' so it can be used in other contexts.
Someone correct me if I'm wrong as this is a good
topic.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last reply is exactly correct. You can overload any method, even main. However, for the program to run, there must be a
public static void main(String[] args)
This (or its variations) is the only signature that java will RUN.
------------------
fogeyman
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
This is what my understanding says about the problem, we cannot override the main method,this is what i think .In the example, the compiler is taking both the methods as separate.This is why it gives no error.And at run time it finds the main method with String arg, and executes it.
But try this:
public class OverridingMain{
static String str1 = "main method with String[] args";
static String str2 = "main method with int[] args";
public static void main(int[] args){
System.out.println(str2);
}}
the compiler produces error:java.lang.NoSuchMethodError
Please correct me, if Iam wrong.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bindesh Vijayan:
Hi everybody,
This is what my understanding says about the problem, we cannot override the main method,this is what i think .In the example, the compiler is taking both the methods as separate.This is why it gives no error.And at run time it finds the main method with String arg, and executes it.
But try this:
public class OverridingMain{
static String str1 = "main method with String[] args";
static String str2 = "main method with int[] args";
public static void main(int[] args){
System.out.println(str2);
}}
the compiler produces error:java.lang.NoSuchMethodError
Please correct me, if Iam wrong.


The reason it does not run is because there is no point of entry for the program. there is no "public static void main(String[] args)" method, which is what is being looked for to execute.
Remember that you were previously overloading not overriding. You have called this class OverridingMain, so I am assuming you may be thinking that you are overriding, but you aren't. In order to override you need at least two classes, a superclass and a subclass.
The OverridingMain class is just a single class with no entry point, so you can't run it. It does not override or overload.
gary

[This message has been edited by Gary Lupul (edited August 24, 2001).]
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Gary, actually I was making this big mistake of assuming overloading as overriding.Thanks a lot.I forgot for a moment.Sorry everybody!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic