Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Lightbulb Two Classes with main each

    hy,

    here is the code which is in one file

    Java Code:
    public class gradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is gradeBook1" );
    	}
    }
    
    public class gradeBook2
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is gradeBook2" );
    	}
    }
    as you see from the above code in which two classes in one file and have each main method . . . My question is
    1. Is it possible that two classes with each main in one file ?
    2. If yes, then through which name this file will be saved either gradeBook2 or gradeBood1 or may be another name ??
    3. And then how we compile this type of file ?

    Thanks . . .

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cluster Storm View Post
    hy,

    here is the code which is in one file

    Java Code:
    public class gradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is gradeBook1" );
    	}
    }
    
    public class gradeBook2
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is gradeBook2" );
    	}
    }
    as you see from the above code in which two classes in one file and have each main method . . . My question is
    1. Is it possible that two classes with each main in one file ?
    2. If yes, then through which name this file will be saved either gradeBook2 or gradeBood1 or may be another name ??
    3. And then how we compile this type of file ?

    Thanks . . .
    You can't have two public top level classes in one source file. Every class (properly stored in its own source file if it is a public class) can have a static void main(String[] args) method.

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    A method named main: public static void main(String[] args) { ...
    can be in ANY class. There is nothing in the compiler restricting it.

    The main() method is the method chosen by the JVM when you issue the java command:
    java Program
    The main() method in the Program class will be called by the JVM. Any other main methods will be no different than any other method in any other class.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Cluster Storm View Post
    1. Is it possible that two classes with each main in one file ?
    2. If yes, then through which name this file will be saved either gradeBook2 or gradeBood1 or may be another name ??
    3. And then how we compile this type of file ?
    As Jos explain to you, it's not possible to define more than one public class in top level of the hierarchy. So you have to save the file only with one public class in a single file.

    It's possibly to define ONLY ONE main method on each class without an error.

  5. #5
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    I did this, is it ok to write like this . . .

    Java Code:
    public class GradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is GradeBook1" );
    	}
    
    
    	class GradeBook2
    	{
    		public static void main( String args[] )
    		{
    			System.out.println( "This is GradeBook2" );
    		}
    	} // end GradeBook2
    
    } // end GradeBook1
    Now when i compile this it gives me an error and i save this file with the name
    "GradeBook1".

    GradeBook1.java:11: inner classes cannot have static declarations
    public static void main( String args[] )
    ^
    1 error

    What the problem have it ? GradeBook1 have its own main and GradeBook2 have its own ?? Please elaborate this . . .

    Thanks . . .

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cluster Storm View Post
    I did this, is it ok to write like this . . .

    Java Code:
    public class GradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is GradeBook1" );
    	}
    
    
    	class GradeBook2
    	{
    		public static void main( String args[] )
    		{
    			System.out.println( "This is GradeBook2" );
    		}
    	} // end GradeBook2
    
    } // end GradeBook1
    Now when i compile this it gives me an error and i save this file with the name
    "GradeBook1".

    GradeBook1.java:11: inner classes cannot have static declarations
    public static void main( String args[] )
    ^
    1 error

    What the problem have it ? GradeBook1 have its own main and GradeBook2 have its own ?? Please elaborate this . . .

    Thanks . . .
    Your GradeBook2 class is an inner (non-static) class in the outer class GradeBook1. Non static classes can not have static methods because the entire class doesn't exist without an object from the outer class.

    kind regards,

    Jos

  7. #7
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Java Code:
    public class GradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is GradeBook1" );
    	}
    
    
    	static class GradeBook2
    	{
    		public static void main( String args[] )
    		{
    			System.out.println( "This is GradeBook2" );
    		}
    	} // end GradeBook2
    
    } // end GradeBook1
    Now this working fine and giving me the output "This is GradeBook1". I want that when i run, this use the GradeBook2 main method Not GradeBook1?? What's the procedure for calling GradeBook2 main method at run time?

    Thanks . . .

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cluster Storm View Post
    Java Code:
    public class GradeBook1
    {
    	public static void main( String args[] )
    	{
    		System.out.println( "This is GradeBook1" );
    	}
    
    
    	static class GradeBook2
    	{
    		public static void main( String args[] )
    		{
    			System.out.println( "This is GradeBook2" );
    		}
    	} // end GradeBook2
    
    } // end GradeBook1
    Now this working fine and giving me the output "This is GradeBook1". I want that when i run, this use the GradeBook2 main method Not GradeBook1?? What's the procedure for calling GradeBook2 main method at run time?

    Thanks . . .
    Well, the GradeBook2 class is a member of the GradeBook1 class, so that would be GradeBook1.GradeBook2, but GradeBook2 is not a public class so it depends a bit on the version of your jvm whether or not you can run its main( ... ) method.

    kind regards,

    Jos

  9. #9
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. compile classes with no main type
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 07-20-2009, 08:01 PM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Classes and main methods?
    By CyberFrog in forum New To Java
    Replies: 17
    Last Post: 05-26-2009, 04:47 AM
  4. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •