When to use anonymous classes? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I run into some code which contains anonymous classes. I haven't met with anonymous classes before, so I did some research on them.
My main area of interest is java, so I checked Oracle's tutorial of anonymous classes. I understand the mechanism and I see the point of the examples but in my opinion using anonymous classes makes the code hard to read and can cause a lot of headache.
Are there any cases when it is unavoidable to use anonymous classes or it is advised to use them instead of named classes?

Are there any cases when it is unavoidable to use anonymous classes
No, you can always just use a private inner class instead of an Anonymous class.
using anonymous classes makes the code hard to read and can cause a lot of headache
This very much depends on how you use anonymous classes. Consider the following example:
new Thread(new Runnable() {
#Override
public void run() {
// do something
}
}).start();
In this example you create a Runnable which is run by a thread. If you wouldn't use an anonymous class you'd have to write it as follows:
private class SomeClass implements Runnable {
#Override
public void run() {
// TODO Auto-generated method stub
}
}
and use it as:
new Thread(new SomeClass()).start();
With the first possibility you can directly see what that thread is doing, in the second possibility you'll first have to find the class that is used here.
Another advantage of anonymous classes. You can do the following:
// define some constant which can be used in the anonymous class:
final String someStr = "whatever";
new Thread(new Runnable() {
#Override
public void run() {
// use the String someStr directly
System.out.println(someStr);
}
}).start();
You can use the constant which is declared in the code where the anonymous class is defined. If you'd use a private inner class you'd have to give these constants to the constructor of the class in order to use them!

You can always avoid using of anonymous classes by defining new class implicitly. However in some cases it's better to use anonymous classes. Consider following example:
void main() {
new Thread(new Runnable(){
public void run() {
System.out.println("Hello world");
}
}).start();
}
Code snippet above is more readable and shorter then defining new class.
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Hello world");
}
}
void main() {
new Thread(new MyRunnable()).start();
}
In Java 8 labmda can be used instead of anonymous class in some cases
Runnable task = () -> { System.out.println("hello world"); };
new Thread(task).start();

Are there any cases when it is unavoidable to use anonymous classes...
No. You could always define a named class for that. The point of annonymous classes is to make the code more concise and compact in situations in which you only need to use a class once.
it is advised to use them instead of named classes?
It depends on the context of the class being created.
if you are implementing interfaces having only one function (i.e Runnable) then using lambda expression instead of anonymous classes is not a bad choice.
lamba expression

Related

When to use refactoring with "Convert Anonymous to Inner"?

Android Studio provides good refactoring items.
But when to do "convert anonymous to inner" is better?
For Example:
new Thread(new Runnable() {
#Override
public void run() {
// do something
}
})
After Refactoring:
new Thread(new MyRunnable());
private static class MyRunnable implements Runnable {
#Override
public void run() {
// do something
}
}
As far as I know, The static method you should use to avoid Memory Leak. Eg:
new Thread(new Runnable() {
#Override
public void run() {
// do something long here
YourActivity.this.doSomething();
}
})
--> above code will make your app get memory leak error when you close your app when the thread is running.
However, with new refactor you can avoid memory leak easily:
private static class MyRunnable implements Runnable {
WeakReference<YourActivity> activity;
#Override
public void run() {
// do something long
if(activity.get() != null){
activity.get().doSomething();
}
}
}
It is much easier to create several instances of an inner class (by calling its' constructor) than creating several instances of an anonymous class.
A much easier question to answer is How are Anonymous (inner) classes used in Java?, which in short is to either quickly implement an interface (or abstract class) without adding much functionality to the interface (or abstract class) vs. wanting a strongly typed sub-class of another class which which will be constructed regularly.
A good example for interfaces/abstract classes you'd usually want to use an anonymous inner class are Runnables and most Listener interfaces, since they only serve as a wrapper for a piece of code "given" to an instance of another class. Good examples for inner classes are Fragments in android or custom controls (View in android), though refactoring those into separate classes can make them much more reusable.
A quick and dirty test is "Do I need create my own constructor for my inner class?", if the answer is "yes" than use a non-anonymous inner class.

Can you implement an interface during initialization?

I was reading through one of Oracle's lambda expression tutorials, and came across the following code:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
public class RunnableTest {
public static void main(String[] args) {
System.out.println("=== RunnableTest ===");
// Anonymous Runnable
Runnable r1 = new Runnable(){
#Override
public void run(){
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
// Run em!
r1.run();
r2.run();
}
}
My question is why didn't they implement Runnable when creating the class? Since they overrode the run method when initializing r1, did that take care of the implementation?
Yes, this is called an anonymous class in Java.
https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
You can implement an interface or extend a class when using the new operator, which will create a new instance of the unnamed subclass you define at the time. It's mostly used when you're writing code to be used in another thread or as a callback, since you only get the one instance.
The new lambda syntax in Java 8 replaces anonymous classes for interfaces with a single method, such as Runnable or the interfaces in java.util.function. This is what they're demonstrating in the example.

What do these curly brackets do?

This is a piece of code that was generated by netbeans and I'm confused as to what those brackets do after it calls AbstractListModel.
li_reminderslist.setModel(new javax.swing.AbstractListModel() {
String[] strings = { };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
Sorry for the beginner question. I tried look for a tutorial on it, but couldn't find one.
It creates an Anonymous Inner Class.
Basically, you're creating a new instance of AbstractListModel, but overriding a few things. Since you're only going to use it in this one place, there's no point in creating a whole separate class declaration with its own name—hence the "anonymous" part.
On a side note, since Java 8 Lambda Expressions serve a similar function to Anonymous Inner Classes, but they're specifically used for instantiating classes/interfaces with only one abstract/unimplemented method.
This is implementation of anonymous inner class.
For example you may create new Runnable:
new Runnable() {public void run() { }});
or ActionListener
new ActionListener(){public void actionPerformed(ActionEvent e}{}};
in the same manner.

Declaring Anonymous Inner class

rb.addActionListener(new ActionEvent(ae) {
public void actionPerformed(ActionEvent ae) {
nowCall(ae);
}
});
Another way
Thread th=new Thread(Runnable r) {
public void run() {
// do something
}
};
// notice the ending of above 2 snippets
I am really confused seeing these two.It seems there is no exact pattern to declare an anonymous inner class.
please explain the syntax for anonymous inner class.
The second isn't valid, as far as I can see and test.
What would be more common would be to create a new Runnable implementation:
Thread th=new Thread(new Runnable() {
#Override
public void run() {
// This implements Runnable.run
}
});
Now you could just override the run method of a normal thread:
Thread th=new Thread() {
#Override
public void run() {
// This overrides Thread.run
}
};
... but personally I prefer specifying the Runnable separately when creating a thread.
Now the difference that you noticed at the end is simply whether the expression is used as an argument (e.g. to the addActionListener method or the Thread(Runnable) constructor, or whether it's just assigned directly to the variable. Think of the whole new TypeName() { ... } as a single expression, and it's just the difference between:
Thread th = expression;
and
Thread th = new Runnable(expression);
There is the difference that in the first case your passing it as an parameter to a method and in the second example you're storing it in a local variable.
So you can't really compare both examples against each other.
[...] notice the ending of above 2 snippets
The trailing ) in your first example is simply a termination of
rb.addActionListener(
(i.e., your two examples have different endings because one is a right hand side of an assignment
Thread th = ... ;
and the other is an argument to a method call
...addActionListener( ... );
The syntax of creating an anonymous class is simply:
new SomeClassOrInterface() {
// implementation goes here
}
Which as you can see is the pattern for both of your examples.
From Anonymous Classes (Java in a Nutshell):
3.12.3. New Syntax for Anonymous Classes
We've already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as:
new class-name ( [ argument-list ] ) { class-body }
or:
new interface-name () { class-body }
Also, you have a typo in your second example. It should probably read new Thread() { ... or new Thread(r) { ... (though in the latter case the overridden method will not be called).
Instanciating new anonymous class and passing the object to addActionListener method.
Instanciating new anonymous class and assigning the object to local variable th
They are both the same. You put the anonymous class right after the declaration, before the semicolons:
new ActionEvent(ae) {class details, methods etc} ;
and
Thread(Runnable r) { public void run() { // do something }} ;
in both cases you create a new instance of the class, in the first example you use it as a parameter to method, and in the second you assign it to a variable.
The difference is that you can either implement an interface as an anonymous inner class or extend a class. In you example both are extending a class

Java Runnable Question

I'm currently taking a course in Java and I've run into some confusing code.
Example:
Runnable runnable = new Runnable()
{
public void run()
{
//doStuff
}
};
I don't really get what this code is doing.
How can the run method be associated with an instance of a class?
I googled "Runnable" and found out that it is an interface. Am I implementing the interface by declaring the run method between curly brackets ? Can this be done for any interface in java ?
I could use some links/explanations. Thank you!
It's an anonymous inner class that's implementing the interface Runnable. Yes, you can implement any interface this way, although there are reasons why you would or wouldn't in any given case (lack of reusability being a big one in the "wouldn't" column). More about anonymous classes here, but it's basically a convenient form of this:
// Define it
class Foo implements Runnable
{
public void run()
{
// Do stuff
}
}
// And then use it
Runnable runnable = new Foo();
...provided Foo is an inner (or "nested") class. More about nested classes here.
yes, you are implementing the interface by declaring the run. Yes it can be done for any interface.
This is typically done when you pass an implementation to a method that expects an argument of an Interface type, and you don't have a declared class that is appropriate. You can just implement the interface on the spot, and that code runs. Pretty neat.
I googled "Runnable" and found out
that it is an interface. Am I
implementing the interface by
declaring the run method between curly
brackets ? Can this be done for any
interface in java ?
Yes!
This code is instantiating an object which implements Runnable. Because we can't actually construct an interface, any code which attempts to do so must provide implementations for the interface's methods in curly brackets. We don't really get to see what class Java is creating to implement Runnable (these are abstract terms).
If you were to do the following:
Runnable runnable = new Runnable()
{
public void run()
{
System.out.println("I'm running");
}
};
runnable.run();
you would see "I'm running" as your output.
In some situation , this sample code will be useful .... test runna = new test()
class test implements Runnable{
test(){
Thread t = new Thread(this);
t.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("asd");
}
}
}

Categories