the corner office

a blog, by Colin Pretorius

« Hardy Heron | Main | Political theatre »

# Grouping classes

I was looking at some sample code today, and found a Java file with about 5 classes in the file - one public, and another 4 default-scoped classes. Although it's perfectly legal, my experience is that you hardly ever see this in production code. You'll see enough inner classes, member and static, often a healthy dose of anonymous inner classes (and almost never a local class), but bundling a number of classes into a Java file? It just doesn't happen.

I wonder why that is. I suspect it has a lot to do with how people work with IDEs. In the old days when crazy people coded Java in Notepad or vi, it might have made sense (and been more convenient) to have multiple classes in the same file (it's common enough in C++ code). With an IDE like Eclipse, and using something like the Package Explorer, files are the top level of abstraction within a package, and I think developers make an easy file = class equivalence.

Arguably, multiple classes in a file allows an extra level of organisation, logically grouping related classes. Whenever I want to do that, though, I favour inner classes. I can scope the classes more tightly, and associations are more explicit. The down side is you can lose some encapsulation. To be honest, I just never think about anything other than class-per-file with inner classes as needed. So is there a place for multiple classes in a file? Is this a useful but much-neglected feature in Java, or does it hurt readability for developers who don't expect to see it? Not sure...

File under: java : {2008.05.28 - 01:37}

Add a comment

Your name (mandatory):

Your email:
Your email address is not displayed
Your home page:

Comment (sorry, no HTML):

Remember details?
Yes No

meta

-home-
about
contact
disclaimer
articles
code
link blog

style: [?]
[plain.dark.blue]

Categories

java
linux
music
notes/domino
personal
politiek
studies
techie
thee_blog
world

RSS Feeds

rssfeed all posts
rssfeed all cmts
rssfeed tech posts
rssfeed tech cmts

Archives

2008.10
2008.09
2008.08
2008.07
2008.06
2008.05
2008.04
2008.03
2008.02
2008.01
2007.12
2007.11
2007.10
2007.09
2007.08
2007.07
2007.06
2007.05
2007.04
2007.03
2007.02
2007.01
2006.12
2006.11
2006.10
2006.09
2006.08
2006.07
2006.06
2006.05
2006.04
2006.03
2006.02
2006.01
2005.12
2005.11
2005.10
2005.09
2005.08
2005.07
2005.06
2005.05
2005.04
2005.03
2005.02
2005.01
2004.12
2004.11
2004.10
2004.09
2004.08
2004.07
2004.06
2004.05
2004.04
2004.03
2004.02
2004.01
2003.12
2003.11
2003.10
2003.09
2003.08
2003.07
2003.06

© Colin Pretorius