aspose file tools*
The moose likes Beginning Java and the fly likes multiple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple classes" Watch "multiple classes" New topic
Author

multiple classes

Kevin Carr
Greenhorn

Joined: Jan 19, 2010
Posts: 19
Hey, I am just beginning to learn java, started a day ago, and I ran into some trouble when compiling a program. It has multiple classes, one of them being the main, so I saved the file as the same name as the main one. when I tried to compile it, the line said to save each class separate. what am i supposed to do?
Shevesh Srivastava
Greenhorn

Joined: Jan 19, 2010
Posts: 3
can you give your code here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Each public top-level class must be defined in a file with the same name and ending with ".java". For instance, public class "String" must be located in file "String.java", etc.

In your case you have two options:
1) remove the public modifier for each class. They will have default access, and you are allowed to have multiple classes with default access in one file.

2) split the Java file such that you have one file for each (public) class. This is my preferred solution, as you also keep your source files smaller.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Carr
Greenhorn

Joined: Jan 19, 2010
Posts: 19
Im going through the book head first java second edition. Im stuck on the guessing game app. There are three classes, one main, and I am unsure of whether to break it into multiple files or not, and how to do that if am to do that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You have three classes in your file, right? Something like this (I'm using random names since I don't have the book):
You would then create three files as follows:
Kevin Carr
Greenhorn

Joined: Jan 19, 2010
Posts: 19
K, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple classes