• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much i had a good look at both the links and its been very helpful.

thanks to everyone

sam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic