This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 classes in one file. Is that possible?

 
Stee Munche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stee Munche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for clarification, one public *top-level* type declaration in a file.
This compiles fine:
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, that's an inner class right?
Thanks
-Jean
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic