This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Technical Term For This Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Technical Term For This Class" Watch "Technical Term For This Class" New topic
Author

Technical Term For This Class

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6



This code is all in a file called AnotherClass.java. What is the technical term for SomeClass? A colleague of mine and I can't seem to find it anywhere. Bruce Eckel's TiJ uses these types of classes all throughout his book.

Thanks.


GenRocket - A Test Data Generation Platform
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Is there actually a technical term? Correct me if I'm wrong, but isn't this just a way to declare package-private classes without having to write a new .java source file? When compiled, these classes just get turned into the normal .class files, just as though they had been written in separate .java files.

So isn't this 'feature' is more of a convenience for writing lots of associated classes in one file - the package-private classes themselves are just normal classes compiled in the normal way. For that reason, I don't think they have, or need, another name.

This does bring up the other question of why we can't write more than one public class in the same file - just compiling the classes into separate .class files? This would work quite well for some small applications or tightly coupled structures; does anyone know of a good reason as to why this is prohibited?


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Gregg Bolinger:
This code is all in a file called AnotherClass.java. What is the technical term for SomeClass? A colleague of mine and I can't seem to find it anywhere. Bruce Eckel's TiJ uses these types of classes all throughout his book.

Thanks.


It's just another top level class. As far as I'm aware it's left to the compiler to decide whether or not a public top level type must be declared in it's own file with the same name.

Excerpt from �7.6 Top Level Type Declarations:
When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

* The type is referred to by code in other compilation units of the package in which the type is declared.
* The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

[ May 04, 2006: Message edited by: Ken Blair ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Charles Lyons:
Is there actually a technical term? Correct me if I'm wrong, but isn't this just a way to declare package-private classes without having to write a new .java source file? When compiled, these classes just get turned into the normal .class files, just as though they had been written in separate .java files.

So isn't this 'feature' is more of a convenience for writing lots of associated classes in one file - the package-private classes themselves are just normal classes compiled in the normal way. For that reason, I don't think they have, or need, another name.

This does bring up the other question of why we can't write more than one public class in the same file - just compiling the classes into separate .class files? This would work quite well for some small applications or tightly coupled structures; does anyone know of a good reason as to why this is prohibited?


You can. See the exercpt in my previous reply. In fact, the JLS strictly prohibits a system where packages are stored in a database from enforcing the (optional) restriction. Not too long ago there was a student who had some IDE/compiler I've never heard of at their school that didn't enforce it either.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The term I've seen used for these is "supporting classes."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Top level class with package access. I don't think it gets any more technical - can you provide more context for your question?


Tony Morris
Java Q&A (FAQ, Trivia)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Thanks everyone. That pretty much clears it up. I'd seen it here and there and a lot in TiJ but never really considered what it was doing.

Reminds me I really need to just break down and read the jvm spec.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Technical Term For This Class
 
Similar Threads
Constructing objects - is there a difference?
what is synchronized singleton ?
Anonymous classes. . .
methods or what?
in what ways can we change this formula..