This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes" Watch "classes" New topic


Jimmian Lin

Joined: May 31, 2008
Posts: 16
hi. I have a question regarding creating classes and saving them within files. When do I know whether to save the definitions of multiple classes in one .java file or whether I should only save one class inside one .java file. For instance, if I create a "people" class, should that be in its own file, or can it be in a file with other classes.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37913
Probably better to keep each class in its own file, unless they are private classes or inner classes. In fact, public classes must go in a file with the same name as the class (for most compilers), so you can't get two public classes into the same file.

By keeping each class in its own file, you (and the compiler) can quickly find out where each class is.
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
Moreover you'll develop many well-written concise classes. Keeping them in separate files is useful as you'll be able to re-use them easily

Nikos' Java blog
I agree. Here's the link:
subject: classes
Similar Threads
how to convert a jar/war file of .class files to a folder containing .java files
Compile Target
Packages in JAVA
Call a method from another class!!