• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MovieTestDrive

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding. I'm using the Head First Java book. All help is greatly appreciated!!! Here is the script:



 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic