aspose file tools*
The moose likes Beginning Java and the fly likes 2 classes in one file. Is that possible? 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 "2 classes in one file. Is that possible?" Watch "2 classes in one file. Is that possible?" New topic
Author

2 classes in one file. Is that possible?

Stee Munche
Greenhorn

Joined: Oct 07, 2005
Posts: 4
Well I wanted to know if I can put two different classes in one file?
If that's possible how do I name the file???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Absolutely. The Java spec. only limits you to one public type declaration in a file (and that file's name must match the type), but this is perfectly doable:

[ February 22, 2006: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
That being said, most people divide code into one top-level class per file. Are you just asking or do you want to place more than one in a file? If so, why?


There is no emoticon for what I am feeling!
Stee Munche
Greenhorn

Joined: Oct 07, 2005
Posts: 4
Well it's just for the sake of knowing. I know that if there is only one class called Foo, therefore we have to name the file Foo.java. So I wanted to know if putting two or more classes in one file is possible and how should I name the file as I know that if a file contain only one class, eg class Foo therefore if I name the file Too.java, there will be an error.
So how should I name the class with multiple classes??? With the one that's gonna use the other classes???
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
As said before you can have as many top-level classes in a source file as you want, but only one that's declared public. The name of the file must match exactly the name of the public class. Remember that Java is case-sensitive.
Stee Munche
Greenhorn

Joined: Oct 07, 2005
Posts: 4
Sorry for repeating the question.
Paul already gave the answer but when reading the first time I think that I didn't read between the parenthesis, was more focuses on the codes he gave.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Just for clarification, one public *top-level* type declaration in a file.
This compiles fine:


Tony Morris
Java Q&A (FAQ, Trivia)
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Tony, that's an inner class right?
Thanks
-Jean
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jean Fore:
Tony, that's an inner class right?
Thanks
-Jean


Yes, it is an inner class - a special type of nested class.
All classes are either nested or top-level.
So the statement that "one top-level public type per source file" can be accurately translated to "one non-nested public type per source file".

There are other types of nested classes (that are not inner classes) that can be public within a public declaration.

http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2 classes in one file. Is that possible?