Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about classes

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate Lockwood wrote: Does this kind of organization have a name? When should it be used?

Nested classes
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49776
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome
    And you will see from the responses that there are probably several “right” answers to this problem.
     
    Nate Lockwood
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7587
    53
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Nate Lockwood
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic