| Author |
Anonymous Objects
|
Didier Varon
Greenhorn
Joined: Jul 14, 2005
Posts: 23
|
|
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: HttpServletRequest 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?
|
http://home.arcor.de/hirnstrom/bewerbung
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 852
|
|
|
There would be no measurable difference. The objects will become eligible for collection when they go out of scope.
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
Ilja Preuss
author
Sheriff
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
author
Sheriff
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
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
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 Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: Anonymous Objects
|
|
|