CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberJan 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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberJan 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
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 6, 2014

    Yes,
    Otherwise you already know class name where to access. In that case no need to have same name.
    Are you sure? This action cannot be undone.
    Cancel
  • Onkar Pathak

    MemberJan 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....
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 8, 2014

    So the class with modifier public which do not have public static void main method
    can have different file name?
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberJan 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?
    Are you sure? This action cannot be undone.
    Cancel
  • Anand Tamariya

    MemberJan 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 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. 👍
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberJan 8, 2014

    Thanks guys now i understood why java public class name should be same as file name😀
    Are you sure? This action cannot be undone.
    Cancel
  • infoj

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Kunal Deshpande

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • msajaa

    MemberJul 9, 2015

    Answer is simple,because to make sure there is no confusion while compiling and running the program.
    Are you sure? This action cannot be undone.
    Cancel
  • msajce

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Gnanam Ramalingam

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • Gnanam Ramalingam

    MemberDec 24, 2015

    name of the file should be named after the class which holds the main function
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJan 10, 2016

    Logical answer: Java was made that way, C++ wasn't 😉.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register