In Java Why Java class name and file name should be same?
i googgled and found some blogs saying
1) class name and file name should be same only when the class is public
but could not understand core reason for this approach?
😖
Member • Jan 6, 2014
Member • Jan 6, 2014
Anoop KumarA public class is meant to accessed by anyone and for the record a class file is byte-code format not in plain-text.
Now how can you access a public class if NameOfTheFile and NameOfTheClass is not same?
Member • Jan 6, 2014
Member • Jan 8, 2014
Member • Jan 8, 2014
Member • Jan 8, 2014
even i have the same doubt?Anoop KumarSo the class with modifier public which do not have public static void main method
can have different file name?
Member • Jan 8, 2014
If and only if packages are stored in a file system (<a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.2" target="_blank" rel="nofollow noopener noreferrer">Chapter 7. Packages</a>), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
- The type is referred to by code in other compilation units of the package in which the type is declared.
- The type is declared public (and therefore is potentially accessible from code in other packages).
This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.
Member • Jan 8, 2014
The -javadoc command processes a source file only when the file fulfills all of the following requirements:-To avoid multiple public classes in one file
The file name prefix (with .java removed) is a valid class name.
This restriction makes it easy for a Java compiler to find a named class within a package. 👍
Member • Jan 8, 2014
Member • Jul 5, 2015
Member • Jul 9, 2015
Member • Jul 9, 2015
Member • Jul 10, 2015
Member • Dec 24, 2015
Member • Dec 24, 2015
Member • Jan 10, 2016