Anonymous inner classes

V

Vikram

Hi All,
I have an anonymous inner class in my code which is getting
called in a for loop as below:

Class X{

public Image getImage() {

return new Image() {

.........
..........
}
}
}


I have a client code which calls the method getImage() in a for loop
as below.

X x = new X();
for(int i=0;i<100;i++){

Image im = x.getImage();

}

Is there any performance isses if we use the anonymous classes this
way? The reason I thought of using is I am not changing the Image
class further, just using the values.
 
T

Tom Anderson

I have an anonymous inner class in my code which is getting
called in a for loop as below:

Class X{
public Image getImage() {
return new Image() {
.........
..........
}
}
}

I have a client code which calls the method getImage() in a for loop
as below.

X x = new X();
for(int i=0;i<100;i++){
Image im = x.getImage();
}

Is there any performance isses if we use the anonymous classes this
way?

No, i don't think so.
The reason I thought of using is I am not changing the Image class
further, just using the values.

That doesn't sound like a great reason to use an anonymous inner class.
Could you explain more about what you're doing in the "........."?

tom
 
V

Vikram

No, i don't think so.


That doesn't sound like a great reason to use an anonymous inner class.
Could you explain more about what you're doing in the "........."?

tom

The interface Image is a java bean with around 20-22 setter getters.
 
T

Tom Anderson

The interface Image is a java bean with around 20-22 setter getters.

Okay. To be honest, i'd write a regular class for this. Using an anonymous
class only saves you one line, so if you're writing 40-44 lines of
content, it's not a big deal. Also, does it have to be an inner class? Is
the definition using the internals of X? If not, writing it as an
anonymous class means that you're putting that 40-odd line definition
inside another class definition, that of X, which just makes X that bit
harder to read, for no good reason.

tom
 
M

Mark Space

Vikram said:
Is there any performance isses if we use the anonymous classes this
way? The reason I thought of using is I am not changing the Image
class further, just using the values.

Well, any time you create a new class, you do use some system resources.
Basic class creation overhead can be small, but some classes may use a
lot of resources in its constructor implementation. And a class like
"Image" strikes me as a prime candidate for sucking a ton of resources
when a new one is created.

Try to return an existing object, or an immutable view of an existing
one. Consider providing a more flexible interface. E.g. remove
"getImage()" and substitute getNewImage() (sucks a lot of resources) and
getImmutableView() (low overhead). Allow the caller to choose which one
is appropriate for them.

With out knowing more about your particular problem (i.e., that profiler
thing) or what else is driving this (code review? colleague giving you
the hairy eyeball?) it's kind of hard to suggest a general case solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top