Java Main()

Discussion in 'Java' started by c_user, Dec 6, 2011.

  1. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    Hi frds,
    can there be more than one main() method in a single program.
    please justify.
    thanking you
    :thinking:
     
  2. guptasandeep

    guptasandeep Banned

    Joined:
    Dec 3, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    yes there can be more than one main method in a single program but in different class.
     
  3. miteshaegis

    miteshaegis New Member

    Joined:
    Nov 8, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    You can use more than one main method in different class.

    thanks
     
  4. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    if you are asking about overloads yes
     
  5. ArnavKumar

    ArnavKumar New Member

    Joined:
    Oct 30, 2013
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    The number of main methods can not be greater than number of classes. Each class can have its own main method. But a class can not have more than one main method.
     
  6. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    a class can, the jvm looks only for the one that matches the correct format
    rest it ignores and throw the NoMainMethodFound kinda exception if you try to run it
     
  7. ArnavKumar

    ArnavKumar New Member

    Joined:
    Oct 30, 2013
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Code:
    
    public class FirstMain {
    
    	protected void main(){
    
    	}
    	public static void main(String[] args) {
    		System.out.println("in first main class");
    	}
    }
    
    class SecondMain{
    		
    	private int main(int a){
    		return a;
    	}
    	
    	protected void main(){
    
    	}
    	public static void main(String[] args) {
    		System.out.println("in second main class");
    	}
    }
    
    class ThirdMain{
    	protected void main(){
    
    	}
    	public static void main(String[] args) {
    		System.out.println("in third main class");
    	}
    }
    
    Here you are having different mains in different classes. Are you looking for this?
     
    shabbir likes this.
  8. tech nerd

    tech nerd New Member

    Joined:
    Oct 14, 2015
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    As far I know you can have more than one main method in one java file but not in the same class.
    There has to be inner classes to have multiple main method.
     
  9. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
    Yes,there can be more than one main method if method parameters (number (or) type) are different. It’s called overloading.
    Like:-
    public static void main(String[] args)

    public static void main(int args)
     
    RRT2010 likes this.
  10. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    98
    Likes Received:
    18
    Trophy Points:
    8
    Location:
    India
    Home Page:
    httpS://persys.in/
    Yes,there can be more then one main() method.
    We just need public static void main(String args[]) to start with and then you can call overloaded main methods inside this main and it should work for sure.
    eg.
    Code:
    public class MultipleMainProgram{
    
      public static void main(String args[]){
         main(1);
         main('c');
         main("MyString");
      }
    
      public static void main(int i){
        System.out.println("Inside Overloaded main()");
      }
    
      public static void main(char i){
        System.out.println("Inside Overloaded main()");
      }
      public static void main(String str){
        System.out.println("Inside Overloaded main()");
      }
    }
     
  11. meenal deshpande

    meenal deshpande New Member

    Joined:
    Oct 11, 2018
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    Well, A java program can contain more than one “main” method in a class but, in the terms of Method Overloading.

    The Standard MAIN method accepts the array of Strings as argument in java.

    Method Overloading- The feature in java that allows more than one function of same name but different argument and return type.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice