Re: Event Handling Styles
To really understand this, you need to really understand the differences between anonymous classes, inner classes (and static inner classes), and "normal" classes.
Keep in mind that inner classes (both anonymous and non-static named inner classes) keep an invisible reference to the outer class, which means that as long as the Listener is in scope, so is the outer class. This might not matter for you, but it's something to keep in mind.
Static inner classes don't have that reference, but they can't access any non-static class members.
Separate, "normal" classes might be overkill if the event handler is only one line of code, but then again keeping around references to the outer class might be overkill if you don't need it.
In the end, just like all programming, there is no one right answer. The "correct" thing to do depends entirely on you, your context, and what you find most maintainable.
Re: Event Handling Styles
I see.
So, you say any class which can see the inner class also has access to the outer class? Is the reverse true too?
Back to my original question, I am doing fine with the anonymous classes actually. Adapters and only the needed methods are good for now.
The main reason I asked this question is, I wanted to keep my GUI class only for GUI as much as possible. I don't know if this helps or prevents something but, it lets me deal with the code better. All my variables in GUI are private. So, getters and setters would create a big code. But then again, it still would be cleaner. I wonder if this would be a good practice in terms of performance (Keeping the GUI class pure GUI as much as possible).
The other thing was the fact that I am not writing a code but I am experimenting. This means I always try to improve and change things. Like, I used text boxes to keep data, then decided to use JList and I had to write my FileSave class from scratch. When you do that, you tend to break things and if I keep every class as pure as I can keep. Then the separate classes seem logical. But like I said, with all those setters and getters...
I recently stopped learning then doing and started learning while doing. I used to (and still do) come here and ask questions how to do, what's the theory etc. But I found that, if you build a project, and improve it in your mind then improve your code to catch your ideas is better for me. But still I have to ask questions like this.
So, while writing this long post, I thought about those three implementations and here is my ideas:
- Anonymous classes are good if you don't use the components for the same or similar purposes. That eliminates the other two options, because you won't be writing less codes with the other two since your components have nothing in common.
- Inner classes would be suitable if your components have something in common, like reading a file, then doing the job. So you could save yourself from writing the file read codes twice or more. Of course, you could just have a fileRead method too...
- The separate classes are the less appealing ones. I really cannot see any advantages of them. Actually, they have serious obstacles; I have to write tens of getters and setters. I have to pass the GUI to the eventHandling class as a parameter and also I have to create another object in the GUI class.
So, in terms of serving my laziness, this is the case. But I also would like to know about their performance and memory terms. I am not writing a resource hungry app right now, but what if I decide to write for a low resource system?
Re: Event Handling Styles
Like I said, inner classes keep around a reference to the outer class. That can be a memory issue. Separate classes are useful in that they don't do that, plus they allow you to invoke other methods on the instance later.
Recommended reading: When To Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: Event Handling Styles
Thanks.
I googled for the terms and read the tutorial.
At least for my present project, I think anonymous classes which I have been using are the best practice. And I just don't need to make my GUI class 'pure' GUI.
I'll poke this thread if I wonder anything else on the subject.