Reputation: 5511
I got into another "philosophical" question regarding Java. It's about the following scenario:
You have a T
class that defines an interface L
that is to be used as a Listener, a Notifier for a second class.
Now you have a second class (in the following code they are the A
and B
classes). That class will create a new object of class T
and use L
which the latter will use to communicate with the first.
So I present 4 different versions of how it the Listener Object can be passed on to T
:
Classes A
define an implementation of L
, the class LL
and use a new object of it to create a T
class.
A1
preallocates the objectA2
creates a new object on the spotClasses B
use the inline way of creating the object through the use of anonymous classes of L
(thanks Tim Bender for the correction)
B1
preallocates the objectB2
creates a new object on the spotMy question is, is any of these versions more efficient in a way or another? Is any of them not safe for some reason? Please discuss, suggest other versions and explain!
Class T
class T extends TT{
public interface L{
public void do(int i);
}
private L Lo;
T(L i){
Lo = i;
}
public void start(){
// do stuff
L.do(0);
}
}
Class A
class A1{
private class LL implements L{
@Override
public void do(int i){
// do stuff
}
}
private LL l = new LL();
public void function(){
T t = new T(l)
}
}
class A2{
private class LL implements L{
@Override
public void do(int i){
// do stuff
}
}
public void function(){
T t = new T(new LL())
}
}
Class B
class B1{
private L l = new L(){
@Override
public void do(int i){
// do stuff
}
};
public void function(){
T t = new T(l);
}
}
class B2{
public void function(){
T t = new T(new L(){
@Override
public void do(int i){
// do stuff
}
});
}
}
Upvotes: 0
Views: 538
Reputation: 20442
The difference between A and B (using anonymous classes) is not a runtime difference. When you compile this code you should find that an anonymous class is created using an identifier like B1$1. You can even examine that class file in the bin directory you compiled to.
The only difference between 1 and 2 is that by creating member variable you are bloating the size needed to instantiate an instance of the containing class. You should only create the member variable if it is needed (for re-use purposes).
Most importantly though, the primary concerns when developing should always be clarity and correctness. If a performance issue later arises, it is much better to approach a functioning application with an analysis tool that will identify the troubled areas. Or, possibly use a static analysis tool to identify small but easy gains (low hanging fruit).
Upvotes: 2
Reputation: 49018
There's really no significant difference in software efficiency. It really boils down to programmer efficiency and the greater context. If you know for sure it's only going to be used once, then B2
is generally the preferred method as it is more concise. A1
is more verbose, but also more reusable. Also, by choosing good variable names, the verbosity tends to lend itself better to self-documenting code if it is at all complex.
My personal tendency is a third option where the outer class implements the interface and this
is passed to the constructor.
Upvotes: 2