I don't see any functionality in an anonymous class.
Could someone explain to me what it can help me with?
Unfortunately, I don't get it. Of course I read its documentation in Oracle, but I don't see a use in production that works, I see it as if it were a method.
Anonymous classes in java are used a lot for example in listeners, callbacks or events. The advantage of an anonymous class is that it can access local variables in its scope.
An example. We're using a Dao that gives us the result of a database request in a ResultCallback .
We want to access this DB in a controller and put the results in the corresponding views.
We could also use any local variable in the scope of the anonymous class that is declared with the final modifier .
In a nutshell, using an anonymous class allows you to create an object that implements a particular interface and be able to use it freely without the hassle of having to explicitly define a class.
Although it is never strictly necessary to use anonymous classes (it is always possible to achieve equivalent code using normal classes), there are at least 3 advantages to using them that I can think of, and which apply under certain circumstances:
To better understand these 3 points, let's look at an example where anonymous classes are typically used: event listeners.
For example, let's say you're working on a project with a visual interface that includes several buttons and you need to define what should happen in the Click events of each button. This is achieved by specifying a
ActionListener
for each button.Using a normal class
Without anonymous classes, you have to define a full class that implements the interface
ActionListener
:... which you can then use like this on the form that has the button:
Although at first glance it doesn't look too bad with a single button, imagine the hassle of having to define a class
ButtonXActionListener
for each button if your form has several.Also, if you're looking at the form code, it's not immediately obvious what happens when you click the button. It is necessary to open the class
Button1ActionListener
to be able to have the complete picture of what is going to happen. Again, if you have multiple buttons, in order to get the full picture of actions, you would need to open and examine multiple different classes.And finally, notice that the action in this case is to modify the text of a field in the form. But since the class
Button1ActionListener
does not have access to the form, it is necessary to define a constructor just to be able to access the field in question. This is not ideal.Using an anonymous class
With an anonymous class, you don't need to define a class apart from the form. You can define the action for the button directly in the form code like this:
Again, imagine that you have to define this type of action for several buttons on your form. We can immediately see the advantages in contrast to the previous example:
actionPerformed()
has direct and convenient access to the variablejText
and any other variables in the form's method or class. This saves you code compared to a normal class (as I mentioned before, to a degree, nested classes can help with this too, but I don't want to get off topic).Using a lambda expression
It is worth mentioning that, as of Java 8, many of the typical uses for anonymous classes can be enhanced by using lambdas expressions instead . Basically, anywhere you happen to work with an interface that has a single method (as is the case with event listeners like
ActionListener
), you can use a lambda expression rather than an anonymous class. So while anonymous classes still have their uses, as of Java 8 there are fewer situations where their use would be favored.Notice how compact the equivalent code looks using a lambda expression :
Once you learn and get used to the lambda expression syntax, you can see that it has all the advantages of anonymous classes but with a much more compact syntax.
The advantage of anonymous classes are many, but the root of all this is the use of so-called "interfaces" which are helpful in creating classes as abstract as possible so that they can be reused later (code reuse). One of those advantages is that by passing an interface as a parameter in an anonymous class, you have the possibility to customize the action to execute and in the process you can use local objects at your fingertips. That way you don't have to create another class that has to fetch them via a catch method:
If not, being in local overwriting the interface method, you only call that local JLabel and do whatever you feel like with it.
And apart from everything you can reuse the code with another different action that you fancy. And so we take advantage of object-oriented which saves us a lot of work and a huge mess in our code. Let's avoid code redundancy.
Sometimes we make classes that almost do the same thing but only change some action or parameters. And it is better to make a single class that supports everything.
It's like a tamale maker who not only knows how to make rajas tamales, but also makes them with mole, green, sweet, jarochos, etc. And such a class is much more powerful. Because he puts up with everything.
I don't know if I made myself understood. Greetings.
In fact, it is a common and current method.
BUT...
That method, which is actually an interface, is going to be sent as a parameter so that another method processes it and makes use of the content of that code.
Obviously when being sent to another method, the function is not only going to be what you are putting in the anonymous class, but it is also going to be used with an extra function:
Like for example executing that method in a different thread.
Which you can't do with a simple method alone. In this case, the Thread Object requires it as a parameter of its run() method so that, by means of an algorithm, that custom action, which can be a hello world or whatever, is executed in a different thread.