aspose file tools*
The moose likes Beginning Java and the fly likes MovieTestDrive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MovieTestDrive" Watch "MovieTestDrive" New topic
Author

MovieTestDrive

Tim Shuman
Greenhorn

Joined: May 12, 2012
Posts: 2
I have a quick question or three concerning the MovieTestDrive. I have to save it using the name "MovieTestDrive" and not Movie right? That's because it needs to be saved under the class with the "main", right? Otherwise I get the error:

Movie.java:11: error: class MovieTestDrive is public, should be declared in a file named MovieTestDrive.java public class MovieTestDrive {

Next, when I do save it under the MovieTestDrive name, no errors. But, when I run it nothing happens. Is something supposed to happen as it is written in the book?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Welcome to the Ranch

Which book?
No, you do not have to have any correspondence between the class containing the main method and the name of the file. There ought to be correspondence between the name of the class and the name of the file. That is usually enforced by the compiler for public top-level classes. So if you write public class Foo, that must be in a file called Foo.java. So you will need at least one file for each public top-level class. You can have separate files for the non-public top-level classes, too, or you can put them in the same file as one of the public top-level classes.

What you think is an ordinary class is a top-level class.

What happens when you run the main method depends on what is written in that main method. Please quote your main method, so we can see what is happening.
Tim Shuman
Greenhorn

Joined: May 12, 2012
Posts: 2
Thanks for responding. I'm using the Head First Java book. All help is greatly appreciated!!! Here is the script:



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
You have got a public class called MovieTestDrive, so that ought to be in a file called MovieTestDrive.java.
You have a non-public (correctly called default access, though a lot of people prefer to say “package-private access”) class called Movie. You can put that inside a file called Movie.java, or inside the MovieTestDrive.java file. If the Movie class is only used by MovieTestDrive, putting the two in the same file sounds logical. You may be able to put Movie elsewhere, in a different .java file, but I wouldn’t recommend that; if it is in a file called XXX.java, you have to write javac XXX.java bfeore you compile the test drive file. Of course, if you change Movie to be public, all that will change.

Is this page 37 in HFJ? There appears to be a line in my copy which you have missed out. After two.rating and before Movie three =. You might get more output if you had that line
You would get a more informative output if you added a toString method to the movie classAdd lines like this to the main method, after you set up all the details for the movie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MovieTestDrive