• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head first java page 36 error  RSS feed

 
Kev Kit
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i followed the instructions in page 36 of head first java the compiler says
[link deleted]

this is my code
 
Kev Kit
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh you're meant to save it as DogTestDrive.java not Dog.java xDD

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes main method is an entry point of program execution, you have to have public static void main(String[] args) method in a class which you want to run using java command.
In your case It seems both classes are in a file so you need to save this file as DogTestDrive.java
 
timothy adigun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kev Kit wrote:oh you're meant to save it as DogTestDrive.java not Dog.java xDD



What is Possible:

You are only required to save your java program file with your "main" class name if and only if, your class which has the main method is declared public.
BUT, if your "main" class that contain the main function is not declared public, you can name your java file any name  HOWEVER, it becomes your responsible to tell java the name
of your file to execute.

E.g
With the example you gave you can do thus:

then



What is better
Make sure your filename is the same with your "main" class name.

Your best option:
Use the modifier "public" with your "main" class name. This help in the sense that the error message you will get will be more meaningful.
And since you are using more than one class in a single java program file, you can only have ONLY one public class per file! 
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I don't have my HFJ to hand, so I can't check the error. But please post the error as text by copying from the command window. Many people are reluctant to click links like that in case they have naughty code in. Please don't be annoyed, but I am going to pull rank and delete the link and ask you to post the error as text.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!