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

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

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

    Edit: rephrasing , it will make you difficult organize your code.
  • micheal john
    micheal john
    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
    Yes,
    Otherwise you already know class name where to access. In that case no need to have same name.
  • Onkar Pathak
    Onkar Pathak
    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
    So the class with modifier public which do not have public static void main method
    can have different file name?
  • micheal john
    micheal john
    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
    That's part of Chapter 7. Packages:
    If and only if packages are stored in a file system (Chapter 7. Packages), 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
    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
    Thanks guys now i understood why java public class name should be same as file name😀
  • infoj
    infoj
    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
    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
    Answer is simple,because to make sure there is no confusion while compiling and running the program.
  • msajce
    msajce
    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
    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
    name of the file should be named after the class which holds the main function
  • rahul69
    rahul69
    Logical answer: Java was made that way, C++ wasn't 😉.

You are reading an archived discussion.

Related Posts

Sony has started off its CES 2014 appearance by unveiling the sibling of the Xperia Z1. The Xperia Z1 Compact embodies most of the features of the flagship Sony Mobile...
Audi, the German automobile manufacturer has launched its premium sportback model in India with Audi RS 7 at a price of Rs. 1.28 Crore (INR 12,856,000) ex-showroom, Mumbai. The luxury...
Sony has left us craving for more by teasing its next wearable technology. Sony’s SmartWear is not a smartwatch but a tiny wearable which will be coupled with an Android...
Intel plans to go inside wearables now, as per the chip-maker's plans revealed at the International Consumer Electronics Show 2014. The company shall create reference design devices and platforms to...
Hey guys Im studying Deploma in automobile engineering. I m in my final year so have to do a project work. i have deffrient ideas. but not sure what to...