More than One Class

If more than one class is present in one Java file then what will be the java file name?

Questions by sahil5

Showing Answers 1 - 13 of 13 Answers

You can name anything. But the class having main method is preferred as the JVM starts running the code from the byte code having the same name as class name of class having main.  Its easy to write same name in front of Java as in front of Javac but without .java extention.

  Was this answer useful?  Yes

If there are more than one class in a file, the file name should be the same as the name of the class with the public modifier.

If there is no class with the public modifier then you can should name the file should be same as the classname in which the main method is implemented.

ramg4java

  • Jul 30th, 2010
 

There can't be more than one public class in the same file. In case more than one public file exists, only the class with the same name as the file name is accessible. If you try to access other classes, an error occurs.


There can be any number of classes in a single file and there is no need for a main function in any of the classes. (Ex: Utility classes)

There can also be main method in each of the classes.

Note that you just mention the file name during compilation, so that the classes are compiled and class files generated. There can be main method in all the classes. and when running the class, you provide the class name, so that the appropriate main method is called. 

However to avoid ambiguity, its better to maintain one class or related classes in a file.

jeetu1607

  • Aug 1st, 2010
 

The Java file should be named same as the class having the main method otherwise NoClassDefFound error will be seen.

  Was this answer useful?  Yes

It is recomended to give the file name of the class which is having main() method.but it is not mandatory.
But the file name must be same with  the class, which is declared as public.

  Was this answer useful?  Yes

msrmsr

  • Oct 5th, 2010
 

If there are more than one class in a java file there is no need of main() in those classes
we can save the file with one of the names of classes.we can compile them but we cant run them.if we want to run them we need a main() with public access specifier

  Was this answer useful?  Yes

nazrulece

  • Nov 15th, 2010
 

//  More than One Class just try to do this
//  the  should be save as    twoMain.java


// when we compile the Class it generate two .class file
// the name.class can not be private or protected ......... it can be public or no modifire

private class twoMain{      // the name class should be public or no modifire ...... and not private or protected.

    public static void main(String a[]) throws Exception{
    
     twoMain1 t=new twoMain1();
     t.ment(); //   t.main(); ... we can not call main() method.
    
     try{
     System.out.println("try");
    
      
     catch(Exception e){
     System.out.println("catch");
    
    
     finally{
     System.out.println("finall");
     }  
    
    }    
    
}


class twoMain1{      //  the class can not be public or protected or private ...... if we declare then we get an error.
//  this should be no modifire.
 
twoMain t1=new twoMain();
//t1.main(); //   t.main(); ... we can not call main() method.
twoMain1(){
System.out.println("twoMain1......twoMain1");
}

    public static void main(String a[]) throws Exception{
     System.out.println("twoMain1");
        
    }
    
    void ment(){
     System.out.println("method......of twoMain1 class.......");
    }
    
    
}

  Was this answer useful?  Yes

sapanindia

  • Nov 15th, 2010
 

Dear sir, satvant sandhu
I declare a normal class and main method is present Here.
public class Xyz
{
public static void main(String[]args)
{
}
}
name of class you have to save your program.
It is not mandatory.
the name of class we have to save a java file, it above cases are mandatory
name of class you have to save your program.
It is not mandatory.
name of class you have to save your program.
It is not mandatory.
Regards
SAPAN


  Was this answer useful?  Yes

annmary

  • Dec 8th, 2010
 

If a file contain more than one class, the name that file will be the name same as the class which is declared as public. It is not mandatory.

  Was this answer useful?  Yes

zshekhtm

  • Dec 10th, 2010
 

You can have more than one class in the same Java source file but only one class can be public in any source file and its name should correspond to the name of a source file. This public class should have main() implemented.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions