• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Access and filename

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the rule that a file can contain at most one public class and that must be in a file named Classname.java
However, I'm not clear on something.
Must a file always contain exactly one public class? Or put another way, can a file contain several classes and none of them be a public class? If so, what filename should be used?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or put another way, can a file contain several classes and none of them be a public class?
Yep.
If so, what filename should be used?
Whatever you want to call it so long as it has a .java extension.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim
"....Must a file always contain exactly one public class? Or put another way, can a file contain several classes and none of them be a public class? If so, what filename should be used? ..."
just small thing that Morris forgets, it's that when no class is public the file name should have the same name as the class that contains main method
example: that's compile and run fine if we put make it at Fethi.java
class Fethi extends Said
{
public static void main(String arg[])
{
// Any Code
}
}
class Said{
void parler(){}
}
but if we call it Said.java, it still be legal, it compiles, but once run, it it throws NoSuchMethodError!
is it Ok?
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic