Alright, I'm using the "HeadFirst Java 2nd edition"
I have some questions about classes.
First, am I to understand that if you create a class file as part of an application, it's intended to be able to be used as part of other applications?
Second, what is the difference between using packages and not?
Third, (and I have looked in other threads for this) why won't my "GameLauncher" run? I keep getting weird error messages like: could not find the main class. See the screenshot attachment.
I compiled from the parent directory using:
and I think it worked.... I'm using the following code for the GameLauncher file:
And this for the Player file:
And this for the GuessGame file:
Filename: GameLauncherFail.bmp
Description: Screenshot of run attempt: GameLauncher
First, am I to understand that if you create a class file as part of an application, it's intended to be able to be used as part of other applications?
It means that you should code in a reusable manner so that various parts of your application could be plugged out, replaced or simply be reused in another application.
It's a way of organizing applications. In bigger projects you have several thousand class files. Having them all in one place is not the greatest thing. It also acts a role when it comes to access modifiers.
Third, (and I have looked in other threads for this) why won't my "GameLauncher" run? I keep getting weird error messages like: could not find the main class. See the screenshot attachment.
Launch your program from the root dir, not a package dir.
I made a 2 new directories and put the java files in the parent and re-compiled to the child directory. It wouldn't run from the parent, but when I ran it from the directory the class files were in it worked....
Probably easier for beginners if you miss out the package declaration from the beginning of each class. I see you had // before "package" in your quoted code.
Very sensible advice to comment out the package name.
If you look at the printed versions in the book, they do not have package names. Package names are required for any large application; if you look in the Java™ Tutorials ("creating packages" you find that
If you do not use a package statement, your type ends up in an unnamed package. Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.
Your game with 3 classes counts as a "small application".
When you download the whole code from the website, however, it is a large amount of code, so you would need package names; you will see the package names correspond to the chapter numbers. That makes it easier to find the code. After all the main reason for packages is to make classes etc easier to find. But compiling such code needs a folder structure to match. There are at least three ways to do it, which I have written about before (and so have several other people on "beginning Java") so you can find them if you do a search. For beginners, it is best to use the easiest instructions for the compiler, so missing out the package names is easier. Then you use the folder you happen to be in as the "unnamed package."
Syntactically there are differences:
If you use package names, you can have several classes with the same name, in different packages.
Thanks a lot! I'm learning Java for a class I'm taking, so I'm trying not to get too far ahead of things.... but sometimes it's good to get a more thorough understanding of things even when it's stuff I don't need to know yet.
I'm a bigger picture learner so I like to know how things should work even if they don't need to.
Please don't add an unrelated question to somebody else's post. This is called hi-jacking and is considered very bad style; you ought to have started a new thread.
Closing.
Post by:autobot
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking