File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Question about classes

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1710
    
  12

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:


[Download GameLauncherFail.bmp] Download



When you do things right, people won't be sure you've done anything at all.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1710
    
  12

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

Joined: Sep 14, 2009
Posts: 1710
    
  12

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

Joined: Jan 01, 2009
Posts: 874


Janeice,

you should run like "java chap02.GameLauncher"
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1710
    
  12

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Sep 14, 2009
Posts: 1710
    
  12

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

    Joined: Sep 14, 2009
    Posts: 1710
        
      12

    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

    Joined: Sep 14, 2009
    Posts: 1
    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

    Joined: Jan 01, 2009
    Posts: 874


    Tyler,
    Welcome to Javaranch.

    Does GuessGame class has the method startGame() ? check it.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question about classes