Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Format of .java file names

Posts: 65002
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody was asking about whether you can have spaces in names.

No, you can't. In Java® identifiers, you are allowed letters and numbers (also _ $£¢€ etc, but you should avoid them in normal identifiers). So you cannot have spaces. You cannot write
public class Hello World
… because the javac tool will see World as a separate identifier and not understand what it means and will fail to compile the code. You must write
public class HelloWorld
instead. And because the class is labelled public you must call the source file Since you can't have two classes with the same [fully‑qualified] name, you cannot write two public classes in the same source file.

“What about names of source files?” somebody will ask. Well, some file systems will permit spaces in file names; ext4 will and I suspect so will NTFS. Can you write file names with spaces in? You would have to have a different name of the class inside the file, because you can't have spaces, and you therefore cannot make the class public, but maybe you can write a package‑private class with a different name. So let's try it.

On ext4, you have to write out the name of the file and the shell will interpret the space as meaning there are two different file, so you have to escape the space. Let's try

campbell@campbellsComputer:~/java$ gedit My\ First\

campbell@campbellsComputer:~/java$ javac My\ First\
campbell@campbellsComputer:~/java$ java Foo
Hello, World!

Well, if anybody can think of anything even more useless to know …
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling GEICO can save you 15% on car insurance in 15 minutes...
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Windows (NTFS) does support spaces in filenames.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!