Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

One Class per file, or all classes in one file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, just a quickie.

Would you say it is better practice to have one seperate Java class file for each defined object/class or would you say it is better to have one file that defines all of the objects/classes and then your main pulls from that.

For example say I was making an old rpg game, would it be better to have:
main.Java
goblin.java
orc.java
fireball.java
telekenisis.java
player.java

or

enemy.java (Contains classes for goblin and orc)
player.java
spells.java (Contains classes for fireball and telekenisis)

Just wondering how you pro's would do it.

Thanks,
James Ritchie
 
Saloon Keeper
Posts: 13391
295
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One file per top-level class always. Also, use proper capitalization for your classes and source files. You should have a class Player in a file named Player.java. Note that you spelled Telekinesis incorrectly.

What makes an Orc or a Goblin so different from the Enemy class that you need separate classes for them?

Don't use plural for class names, unless it's a utility class. Your Spells class should probably be called Spell instead.
 
James Ritchie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,

The orc and goblin were just random examples that came into my head for the sake of the question.

Thanks for your answer.

Yours,
James Ritchie
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, I have about 700 classes in the app that I wrote and still use. Would you advise me to put them all in one file?
 
Saloon Keeper
Posts: 24552
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are specific limitations on defining multiple classes in a class file and in Beginning Java you shouldn't be even trying.

Actually, this is the Java Way:

src/
  com/
     jamesritchie/
        fantasygame/
            Main.java
            enemy/
                 Goblin.java
                 Orc.java
            spell/
                 Fireball.java
                 Telekenesis.java
            weapon/
                 sword/
                      heroic/
                           Glamdring.java
                           Orchrist.java
                      singing/
                           Sinatra.java

(etc)

Note that elements ending with a "/" in the above are directory names. Also note that except for the "src" directory, the sub-directories are all packagename components. So, for example, Glamdring would contain the following declararation:


Which you could import or request by its fully-qualified classname of com.jamesritchie.fantasygame.weapon.sword.heroic.Glamdring.

Note that even under Windows, all file and directory names are case-sensitive in Java. Also that package name components must start with lower-case. In fact, in Java, only constants (sometimes!) and class names should start with an upper-case letter as a general rule. There are tools that are expecting this practice, even though it's not part of the actual Java specs.
reply
    Bookmark Topic Watch Topic
  • New Topic