Forums Register Login

Question about classes

+Pie Number of slices to send: Send
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
File size: 959 Kbytes
[Download GameLauncherFail.bmp] Download Attachment
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thanks for your reply Sebastian....

I just tried running GameLauncher from the parent directory using the syntax:
java chap02 GameLauncher

I got a similar error message like in my screenshot.... "could not find main method"

-Janeice
+Pie Number of slices to send: Send
Alright -

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....

**shrug**

Thanks,
Janeice
+Pie Number of slices to send: Send

Janeice,

you should run like "java chap02.GameLauncher"
+Pie Number of slices to send: Send
Thanks!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
One of the other threads said to comment out the package.

What's the difference in syntax (or behavior), if any, when running it?
+Pie Number of slices to send: Send
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.
  • You can only import a class which has a package name: see Java™ Language Specification §7.5
  • +Pie Number of slices to send: Send
    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.

    Thanks again!
    Janeice
    +Pie Number of slices to send: Send
    Hey Guys (and Girls) I am working on the same program but I am having trouble getting the GameLauncher Class to even compile for me. I get this:

    c:\Users\User\Documents\Java Programs\GuessGame>javac GameLauncher.java
    GameLauncher.java:4: cannont find symbol
    symbol : method startGame()
    location: class GuessGame
                game.startGame();
                        ^
    1 error

    Any help would be greatly appreciated,

    Tyler
    +Pie Number of slices to send: Send

    Tyler,
    Welcome to Javaranch.

    Does GuessGame class has the method startGame() ? check it.
    +Pie Number of slices to send: Send
    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.
    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
    https://gardener-gift.com


    Reply locked
    This thread has been viewed 1435 times.
    Similar Threads
    GuessGame errors
    Head First Java Book, Chapter 2
    Please Help with Guessing Game (Head First Java)
    Exceptation in thread "main" java.lang.NoSuchMethodError: main
    Very new to java
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 07:26:09.