*
The moose likes Beginning Java and the fly likes Multiple class files or one sigle one! Which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple class files or one sigle one! Which is better?" Watch "Multiple class files or one sigle one! Which is better?" New topic
Author

Multiple class files or one sigle one! Which is better?

Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
hi just a quick question,

I've just had to move some of my ActionListerners into the same file as my Main class to get my program to work (they needed to refrence the same global varible). My projects now working again but i was just wondering, is it better to have just one big class file or split the program into as many class files as possible?

Thanks in advance,

sam
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Samuel Woodhams wrote:I've just had to move some of my ActionListerners into the same file as my Main class to get my program to work (they needed to refrence the same global varible).

I don't understand this. First of all, Java has no global variables, so what you do you mean by a "global variable"? And Java doesn't work with compilation units in the same way that C or C++ work; you don't need to put source code in the same file to be able to access variables, classes or methods.

By "global variable", do you mean a static member variable? And by "moving into the same file", do you mean that you put those ActionListeners inside your Main class, as nested classes?

Have a look at this:

In another file:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
ok wasn't really using the corrct terms i was referencing the use of a "public static variable" as a "global variable, and yes i was talking about using nested classes.

but i want to say thanks very much because i couldn't find any were how to refferance a varible in another file so thankyou for that.

but back to my origanl question is it better to have a main class with lots of nested classes in it or to have those classes in a differrent file?

cheers,
sam
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

In my opinion it's better to put them in separate classes than to create one big monster class which contains the whole world.
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
but is it just a chose you personally make, or is it conciderred better programming to do it one way or the other? (i agree with you by the way but i though i would just play devils advict)

cheers

sam
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
As a rule of thumb, you usually want lots of little classes. Remember ActionListeners are little classes in their own right, but they often work better as anonymous classes.

It is sometimes, however, impossible to achieve the number of classes you would like. I used to fulminate about Listeners: look at this old posting and I also once linked to this tutorial.
I hope those links are helpful.
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
thanks very much i had a good look at both the links and its been very helpful.

thanks to everyone

sam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple class files or one sigle one! Which is better?