• Post Reply Bookmark Topic Watch Topic
  • New Topic

Format of .java file names  RSS feed

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • 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 HelloWorld.java. 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\ Class.java
campbell@campbellsComputer:~/java$ javac My\ First\ Class.java
campbell@campbellsComputer:~/java$ java Foo
Hello, World!
Well, if anybody can think of anything even more useless to know …
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling GEICO can save you 15% on car insurance in 15 minutes...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Windows (NTFS) does support spaces in filenames.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!