• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic hello world query  RSS feed

 
hari shankar bhatt
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! I am starting to learn java , here is a simple question that I want to ask !

If suppose we write a simple hello world program

And save this as test.java.
Now after compiling it a helloWorld.class file is generated.


But if we compile the same after adding "public" in front of 1st line, it throws error.

but then changing the file name to the name of the class i.e. helloWorld.java, corrects the error. Why is it so ?


[ps: Sorrry for making the question so long, I wanted to put my question as clear as possible. Thanks you.]
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source of every public class must be in a file named exactly as the class and have .java extension.
This is by design.
 
hari shankar bhatt
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Source of every public class must be in a file named exactly as the class and have .java extension.
This is by design.


thnk you..
 
David McMonigle
Greenhorn
Posts: 24
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you're making a Java file to run in command line, the class name and file name must be exaclty the same.



That code must be saved in a file called HelloWorld.java. After you compile it, obviously you would just write "java HelloWorld"


Here is my site for more reference.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David McMonigle wrote:Whenever you're making a Java file to run in command line, the class name and file name must be exaclty the same. . . .
That applies whether you use the command line or an IDE but only to public top‑level classes.
 
David McMonigle
Greenhorn
Posts: 24
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
David McMonigle wrote:Whenever you're making a Java file to run in command line, the class name and file name must be exaclty the same. . . .
That applies whether you use the command line or an IDE but only to public top‑level classes.


You're right. The fact that you're using command line or not doesn't factor in at all. I was just stating that the class and file name need to be the same in order for a public class file to compile.
 
hari shankar bhatt
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
David McMonigle wrote:Whenever you're making a Java file to run in command line, the class name and file name must be exaclty the same. . . .
That applies whether you use the command line or an IDE but only to public top‑level classes.



Is there any particular reason why .java files with public class should have same file name where as if class is not declared public it can use different file name and still can generate same .class file.???
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that the Java Language Specification says so, and that's probably the most satisfying answer you'll get :-)
 
David McMonigle
Greenhorn
Posts: 24
Chrome Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, one more bit of info, and this really is just nitpicking, but it'll help you in the long run.

Make sure your classes start with capital letters and are CamelCase. So your class name should be "HelloWorld" and you file name "HelloWorld.java".

It's nitpicky but it'll help for referencing reasons, it's also standard naming convention for classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!