Results 1 to 9 of 9

Thread: Overload main method

  1. #1
    Geek_Guest
    Guest

    Overload main method

    How to overload the main method? I know that main() can be overloaded but don't know how to invoke overload version.

    public class A{
    public static void main(String args[]){
    System.out.println("Hi");
    }
    public static void main(int a){
    System.out.println("Hi i m int");
    }

    }

    main() is overload in the above code but how to invoke the overloaded version?

    Question asked by visitor ashok agnihotri


  2. #2
    Junior Member
    Join Date
    Nov 2007
    Answers
    1

    Re: Overload main method

    No answer sorry

    Last edited by marinpradish; 11-24-2007 at 03:17 PM. Reason: It is not the right answer it is about reversing a string without using reverse() method

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Answers
    1

    Re: Overload main method

    Hi
    At first the JVM run the public static void main(String args[]) method. Then second the main method is called the any object and class.


  4. #4
    Junior Member
    Join Date
    Jun 2007
    Answers
    7

    Re: Overload main method

    public class FirstExample{
    public static void main(String a[]){
    System.out.println("First");
    main(1);
    }
    public static void main(int i ){
    System.out.println("Second");
    }
    }


  5. #5
    Junior Member
    Join Date
    Nov 2006
    Answers
    3

    Re: Overload main method

    I think generally to invoke the overloaded method we pass the type of argument to it.

    here also if we pass string as argument it will invoke the first main method and if we pass int value as argument then senod main method will be invoked..

    I am not sure about the answer
    Please correct me if I am wrong.

    Thanks in advance


  6. #6

    Smile Re: Overload main method

    public class FirstExample{
    public static void main(String a[]){
    System.out.println("First");

    FirstExample.main(100);
    }
    public static void main(int i ){
    System.out.println("Second");
    }
    }


  7. #7
    Junior Member
    Join Date
    Jul 2007
    Answers
    12

    Re: Overload main method

    i agree with the reply from Rajan. That is the way to overload main()

    Even if u give any command line argument other than String type, JVM will consider it as String only..and obviously it starts the main() method with argument String.

    Because the JVM will check for the method signature of main with String args...and starts execution from that main only...


  8. #8
    Junior Member
    Join Date
    Aug 2010
    Answers
    1

    Re: Overload main method

    Quote Originally Posted by gaurav0252 View Post
    public class FirstExample{
    public static void main(String a[]){
    System.out.println("First");
    main(1);
    }
    public static void main(int i ){
    System.out.println("Second");
    }
    }
    ...........Thanks i m also searching same


  9. #9
    Junior Member
    Join Date
    Aug 2010
    Answers
    1

    Re: Overload main method

    Hii....


    Definetly y not ...

    Not only main method We can overload any method in a class except 'final' , nobody will stops us , but what is the use by overloading the main method.
    if we overload the main method then its just acts as a normal method.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact