Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Question about classes

 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator

Janeice,

you should run like "java chap02.GameLauncher"
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks!
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
One of the other threads said to comment out the package.

What's the difference in syntax (or behavior), if any, when running it?
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  •  
    Janeice DelVecchio
    Saloon Keeper
    Posts: 1807
    12
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
     
    Tyler Smith
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
     
    Balu Sadhasivam
    Ranch Hand
    Posts: 874
    Android Java VI Editor
    • Mark post as helpful
    • send pies
    • Report post to moderator

    Tyler,
    Welcome to Javaranch.

    Does GuessGame class has the method startGame() ? check it.
     
    Campbell Ritchie
    Sheriff
    Posts: 48404
    56
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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.
     
      Bookmark Topic Watch Topic
    • New Topic