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 Confused on example in chapter 2 of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused on example in chapter 2 of "Head First Java"" Watch "Confused on example in chapter 2 of "Head First Java"" New topic
Author

Confused on example in chapter 2 of "Head First Java"

mark leeds
Greenhorn

Joined: Jun 09, 2009
Posts: 3
Hi: I'm new to java and I'm using Head First Java and the book is really great. But there's an example in chapter 2 ( pagees 39-40 ) that requires 3 classes.

One starts out as public class GuessGame.
The second starts out as public class Player.
The last starts out as public class GameLauncher and under it is the public static void main (String[] args)

The part I don't understand is that, uptil now, whenever I had a class XXX and a main inside it, I called the file XXX.java and then I did

javac XXX.java to compile which created XXX. class and then
java XXX to run it.

But now there are 3 classes so am I supposed to put them each in a seperate file or are all of them supposed to go in the same file. Then, even if I know this,
I'm not sure what to name the file(s) or how to handle the javac step or the java step. Thanks a lot for any enlightenment. It's much appreciated.






Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

mark leeds wrote:But now there are 3 classes so am I supposed to put them each in a seperate file or are all of them supposed to go in the same file. Then, even if I know this,I'm not sure what to name the file(s) or how to handle the javac step or the java step. Thanks a lot for any enlightenment. It's much appreciated.


Rule is you can have only one "public" class per source file. So you may put other two classes in seperate files (and compile them using "javac *.java") or simply remove the "public" from them and keep in the same file and do what you are used to do .

And Welcome to JavaRanch


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
mark leeds
Greenhorn

Joined: Jun 09, 2009
Posts: 3
Thank you Vijitha: But , if I put them each in seperate files , then do I compile them all seperately using javac ? if so, how are they going to know about each other ? In other words, how do I run the full program. Thanks and I'm sorry for my bad questions.


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

mark leeds wrote:But , if I put them each in seperate files , then do I compile them all seperately using javac ? if so, how are they going to know about each other ? In other words, how do I run the full program. Thanks and I'm sorry for my bad questions.


Just put them in a same folder and move to that folder from the prompt and type "javac *.java" (assuming you have not used any package statements so the default package will be used). And to run, type "java ClassnameWithMain".
mark leeds
Greenhorn

Joined: Jun 09, 2009
Posts: 3
Thank you very much jivitha. I will try that and see how it goes. My understand is that
because the compiled code ( the class files ) will all be in the same directory, then
when one does java classwithmain, it will find the other classes ( because they are in the same directory ) and be able to know about them and therefore run the full program. Is that correct ? Thanks so much for your help.





Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

mark leeds wrote:My understand is that because the compiled code ( the class files ) will all be in the same directory, then
when one does java classwithmain, it will find the other classes ( because they are in the same directory ) and be able to know about them and therefore run the full program. Is that correct ?


Yes, with all the class files with default package (all in the classpath i.e: current directory) it should work fine. And when you get used to it start using packages .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused on example in chapter 2 of "Head First Java"
 
Similar Threads
How can a Class create an instance of itself??
catlle drive oop-1 to 4
JAXB Error
Head First Servlets: Can't run test class on BeerExpert servlet
Java code to compile and run another java program