• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Foo.java and the Bar class needs to be in a file named Bar.java
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marianne,

Welcome to JavaRanch!

Each public class should be in its own file named ClassName.java (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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() {
super();
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() {
super();
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 test.foo", the directory would need to be {your-root}\test\foo\Test.java)

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

C:\home\examplesjava>

Hope this helps. let me know if it doesn't, or you have any questions.
 
Marianne Ferrante
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic