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 ]
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
posted
0
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: 2341
posted
0
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
posted
0
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
posted
0
Just for clarification, one public *top-level* type declaration in a file. This compiles fine:
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.