1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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:
    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:
    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:
    0
    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:
    3
    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.

Share This Page