This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Anonymous Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anonymous Object" Watch "Anonymous Object" New topic
Author

Anonymous Object

Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Experts,

In Java we have a concept of Anonymous objects like

new Circle(); // (A)

or

System.out.println("Area is " + new Circle(5).getArea()); // (B)

In both these cases the constructor is called and hence the object should be created BUT as no handle/reference variable is attached to this one then it must be picked by the Garbage Collector.
And if this is the case then how can one really print the area as in the above e.g (B)

Please clarify this point.

Thanks in advance!!!
Faraz
Christiaan Lombard
Greenhorn

Joined: Nov 22, 2009
Posts: 26
Right after the object is created and used it will be eligible for garbage collection, but only after it was used (After: System.out.println("Area is " + new Circle(5).getArea());).


SCJP 6
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
In many cases anonymous objects are anonymous only to you. For example, in:
both the anonymous Thread and anonymous MyRunnable are very alive
and active until the run() method completes. Also, they are given names
in the constructor parameter list.

Jim ... ...


BEE MBA PMP SCJP-6
Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Thanks for this very useful reply.

Regards,
Faraz
 
GeeCON Prague 2014
 
subject: Anonymous Object