In Java Why Java class name and file name should be same?

micheal john

micheal john

@micheal-john-l1fIn3 Oct 26, 2024
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?

😖

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jan 6, 2014

    A 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?

    Edit: rephrasing , it will make you difficult organize your code.
  • micheal john

    micheal john

    @micheal-john-l1fIn3 Jan 6, 2014

    Anoop Kumar
    A 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?

    yes i agreee,

    but my question is "class name and file name should be same only when the class is public?" is it true
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jan 6, 2014

    Yes,
    Otherwise you already know class name where to access. In that case no need to have same name.
  • Onkar Pathak

    Onkar Pathak

    @onkar-Dyydcl Jan 8, 2014

    In java, the class which contains public static void main that name and file name should be same.. because all other classes are accessed through main class and by giving file name same as class containing main you are refering to main.. hence it needs to be same....
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jan 8, 2014

    So the class with modifier public which do not have public static void main method
    can have different file name?
  • micheal john

    micheal john

    @micheal-john-l1fIn3 Jan 8, 2014

    Anoop Kumar
    So the class with modifier public which do not have public static void main method
    can have different file name?
    even i have the same doubt?
  • Anand Tamariya

    Anand Tamariya

    @anand-tamariya-DnfjEX Jan 8, 2014

    That's part of <a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6" target="_blank" rel="nofollow noopener noreferrer">Chapter 7. Packages</a>:
    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.
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jan 8, 2014

    There is no relation between public class/filename and public static void main. main method is just starting pointer of a java class (if class have this method with valid signature).

    Adding few more points to @#-Link-Snipped-# #-Link-Snipped-#
    -To ease javaDoc -
    The -javadoc command processes a source file only when the file fulfills all of the following requirements:
    The file name prefix (with .java removed) is a valid class name.
    -To avoid multiple public classes in one file

    This restriction makes it easy for a Java compiler to find a named class within a package. 👍
  • micheal john

    micheal john

    @micheal-john-l1fIn3 Jan 8, 2014

    Thanks guys now i understood why java public class name should be same as file name😀
  • infoj

    infoj

    @infoj-io05KE Jul 5, 2015

    why even that restriction to have the same name as class name at run time.
    Answer is that is how java interpreter will know which class to load and where is the entry point (main() method) otherwise interpreter may have to scan a lot of class files to determine where to start.
  • Kunal Deshpande

    Kunal Deshpande

    @kunal-jIgtGq Jul 9, 2015

    As long as you don't have a public class in your source file, you can name your source file to any name and can compile. But, if you have a public class in your source file, that file should have the name same as your class name. Otherwise, compiler will throw an error.
  • msajaa

    msajaa

    @msajaa-u9OG7Q Jul 9, 2015

    Answer is simple,because to make sure there is no confusion while compiling and running the program.
  • msajce

    msajce

    @msajce-1SfF7s Jul 10, 2015

    We say this statement that the file name should be same as the class name to make sure there is no confusion while compiling and running the program.
  • Gnanam Ramalingam

    Gnanam Ramalingam

    @gnanam-ramalingam-FIEX4Z Dec 24, 2015

    The compiler will generate a .class file with the name of the class at the time of executing we need to specify the name of the class to be executed i think this can be a one of the reason to name the source code file with its class name...............this is what content i have refered
  • Gnanam Ramalingam

    Gnanam Ramalingam

    @gnanam-ramalingam-FIEX4Z Dec 24, 2015

    name of the file should be named after the class which holds the main function
  • rahul69

    rahul69

    @rahul69-97fAOs Jan 10, 2016

    Logical answer: Java was made that way, C++ wasn't 😉.