Same name for class and file. Why?

Why do we need to save a file with a name similar to the class name of an open class?

public class Indian{ 
     public static void main (String args []){
          System.out.println ("i am a programmer");
     }
}

This class must be saved with the name Indian.Why, so is this a convention?

+4
source share
2 answers

Firstly, this applies only to public classes; Package-private classes can be included in files of any name. For public classes, this is an agreement, in the same order as placing files in directories that reflect the structure of your packages, and using the extension .java.

This is done so that the Java compiler can find classes without examining all the Java files in the folder.

, : Java , , , , Java . Java - .

+2

, Java. ? . 300 , .

+5

All Articles