It's not a secret anymore!
The moose likes Beginning Java and the fly likes What name to use to save files; what classes are allowed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What name to use to save files; what classes are allowed?" Watch "What name to use to save files; what classes are allowed?" New topic

What name to use to save files; what classes are allowed?

Marianne Ferrante

Joined: Feb 20, 2005
Posts: 3
If there was a forum for less than a beginner, I'd have posted my question there. I can never make sense of java so Head First Java seemed right up my alley - except I'm stuck on an exercise in chapter two! I searched the book up to that point and can't seem to find an explaination on how to name files and what can be in one file and what has to be separate.

There are three public classes with one that that has the main program. The compiler complains when all are in the same file (I named it after the class that has main in it.) It says that public classes have to be in separate files.

Yet when I same all three classes in separate files, the ones that need to create objects from the other class can't compile because it doesn't know what the class is and I don't know yet how to do that.

So, can I have one file with three public classes as shown in the book example? And if not, how do I run them in separate files?

Sorry for being so ignorant - sometimes it's those little things that trip me up.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Each public class needs to be in a file of the same name. In other words, the Foo class needs to be in a file named and the Bar class needs to be in a file named

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Marianne,

Welcome to JavaRanch!

Each public class should be in its own file named (where, to be painfully specific, you should replace "ClassName" with the name of the public class in that file.) Then, assuming the files are in the current directory, you can compile them by using

javac *.java

[Jess in Action][AskingGoodQuestions]
Jeremy Thomerson

Joined: Feb 17, 2005
Posts: 3
True, you can only have one public class per file. What you need to do is put all your public classes each in their own file, the name of which should be identical to the name of the class. Make sure they are also in the same directory, and that the package name is the same in each. Here's an example that I hope will help.

// My first class:
package test;

public class Test1 {

public Test1() {
System.out.println("hi, I'm test 1");

public static void main(String[] args) {
Test1 one = new Test1();
Test2 two = new Test2();

// My second class:

package test;
public class Test2 {

public Test2() {
System.out.println("Hi, I'm Test2");

These files would need to be in a directory named "test", because the directory name must match the package name, which in the case of these two classes is test. (If the package were "package", the directory would need to be {your-root}\test\foo\

Then, from the command line you can compile and run these classes.
(My files are both in C:\home\examples\java\test)

compile any files ending with the java extension:
C:\home\examples\java>javac .\test\*.java

run the class test.Test1 (test is the package, Test1 the class)
-cp . means that your classpath is set to the directory you are in, so when you specify package test, class Test1, it will look for the compiled Test1.class file in {your-current-directory}\test\Test1.class
C:\home\examples\java>java -cp . test.Test1
hi, I'm test 1
Hi, I'm Test2


Hope this helps. let me know if it doesn't, or you have any questions.

Jeremy Thomerson<br />Software Engineer, eBay, Inc.
Marianne Ferrante

Joined: Feb 20, 2005
Posts: 3
Thank you all for your help. I will try revising my code. I guess I was too literal in copying the book's example; maybe they never intended for that to happen but hey, when you're a greenfoot you do some pretty stupid things (even when I know how to program in other non-object languages).

I look at Java like when I quit smoking - took me several tries to actual kick the habit. I've tried to get Java a couple of other times, I hope this is the time that I'll actually get the hang of Java! I better or I'll get left in the dust.
I agree. Here's the link:
subject: What name to use to save files; what classes are allowed?
It's not a secret anymore!