File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about 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 "Question about classes" Watch "Question about classes" New topic
Author

Question about classes

Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
I recently came across some java code that apparently had a three classes in a .java file. The first class, let's call it Foo had the same name as the java file and the other two classes implemented Runnable and were helper classes for Foo but the code came after the last closing curly bracket of Foo.

I didn't know that this was allowed. Does this kind of organization have a name? When should it be used?
Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

I came across this awhile back myself. I had wanted to do it for the purpose of showing off to my friends the length of code that was involved without having to open a bunch of text files.
And from what I've seen, that's the only major use for it, to show it off in one place.
The most common place I've seen this done is in book examples where they want to stuff an example onto a single page.
Try Googling "multiple classes in one Java file". You'll find most people don't recommend it.

Best,
Caleb
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Nate Lockwood wrote: Does this kind of organization have a name? When should it be used?

Nested classes


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39394
    
  28
Actually it is appropriate to have several top‑level classes in one file provided . . .
  • 1: Only one of them is public
  • 2: The public class has the same name as the file
  • 3: The package-private classes are used by the public class
  • That sort of thing is a design decision; you can have several classes per file or give them each their own file with the same name as the class. “You pays yer money and you takes yer choice.” I won’t say either approach is wrong.
    Nate Lockwood
    Ranch Hand

    Joined: Feb 22, 2010
    Posts: 83
    Thanks, I did find references through Google and see that it's also carryover from ancient times when Java didn't support nested classes. It appears to be one appropriate solution when the classes are only used by the "main" top level class in the file..
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39394
        
      28
    You’re welcome
    And you will see from the responses that there are probably several “right” answers to this problem.
    Nate Lockwood
    Ranch Hand

    Joined: Feb 22, 2010
    Posts: 83
    Campbell Ritchie wrote:You’re welcome
    And you will see from the responses that there are probably several “right” answers to this problem.


    Oh, yes, and in a few minutes I'm going to implement one of them!
    Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 4706
        
        7

    As a design decision motivated by the relationships between classes, it's a valid practice. As a way to simply avoid having to open multiple files, it's a horrible practice.


    Junilu - [How to Ask Questions] [How to Answer Questions]
    Nate Lockwood
    Ranch Hand

    Joined: Feb 22, 2010
    Posts: 83
    Junilu Lacar wrote: As a way to simply avoid having to open multiple files, it's a horrible practice.


    Yes, and I can't think of any good reason to do that.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question about classes