It's not a secret anymore!
The moose likes Performance and the fly likes Anonymous Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Anonymous Objects" Watch "Anonymous Objects" New topic

Anonymous Objects

Didier Varon

Joined: Jul 14, 2005
Posts: 24
I have been using the following syntax:

HttpServletRequest request;
int userID = (new UserUtil(connection)).findByUserName(request.getRemoteUser()).getUserID();

I know that is not the cleanest code to read, but I have the idea that is efficient, because there are not new references created on the heap, then the anonymous object is ready for garbage collection. Now if I do something like:

UserUtil userUtil = new UserUtil(connection);
String userName = request.getRemoteUser();
User user = userUtil.findByUserName(userName);
int userID = user.getUserID();

will be as efficient that the code describe first?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

That code doesn't look like if it was 100.000 times per minute, so I guess you don't earn to much performance-improvement from it.

Without seeing sourrounding code, it's not possible to say much about the possibility of GarbageCollection to profit from an anonymous Class.

Usually it's a bad idea to help the garbage-collector.
People who liked to help it often fail to show a real-world example, which is impressing.
I'm not impressed so far.
How big is that UserUtil?

Impress me!

Isn't that code-block left shortly later although?
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
There would be no measurable difference. The objects will become eligible for collection when they go out of scope. - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I agree with Steve and Stefan - don't worry about it, just use the one that you find easier to read.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
By the way, all objects are "anonymous" in the sense that they don't have names. The only things having names are the reference variables (which an object can have an arbitrary number being referenced by).
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Now think about this - suppose you got a null pointer exception from the line

How would you inform the operator or the user what the problem was?
Veterans of this forum know whats coming next.

Premature optimization is the root of all evil

Do a search for that phrase in Google

I agree. Here's the link:
subject: Anonymous Objects
It's not a secret anymore!