• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

class name and source file name

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read it is mandatory to have the same name for the public class as the source filename for the program to compile. I also read it somewhere that it is a compiler constraint to have the same name at both the places. Could someone please give me an insight into it. Why exactly same name is needed, is it tht the compiler searches for a class with the same name for the starting point of the file?
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a good Q...why is that there should not be two classes that are public be declared in the same file;whereas it is fine if one is public and other not?

Thx
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the file must have the same name as the public class contained in it, it's impossible to have more than one public class in a file.
If you put only non-public classes in a file there's no restrictions to filenaming.

It is a good practice to not have more than one class per file though (not counting inner classes) and to always give the file the name of the class. Makes it so much easier to find the right files when you need to change a class

It's a requirement laid down in the JLS and/or JVMS. Those form the bible which determine the behaviour of the JVM and compiler. As gospel they don't have to be logical or reasonable (though by and large they are) and no explanation of their reasoning need be given.
 
Fire me boy! Cool, soothing, shameless self promotion:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic