This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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?
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.
Joined: Sep 01, 2009
thanks very much i had a good look at both the links and its been very helpful.