1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Java Main()

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

  1. c_user

    c_user New Member

    Hi frds,
    can there be more than one main() method in a single program.
    please justify.
    thanking you
    :thinking:
     
  2. guptasandeep

    guptasandeep Banned

    yes there can be more than one main method in a single program but in different class.
     
  3. miteshaegis

    miteshaegis New Member

    Hi,

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

    thanks
     
  4. ManzZup

    ManzZup New Member

    if you are asking about overloads yes
     
  5. ArnavKumar

    ArnavKumar New Member

    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

    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

    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

    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

    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