File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Object

 
Faraz Alig
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());).
 
Jim Hoglund
Ranch Hand
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Faraz Alig
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this very useful reply.

Regards,
Faraz
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic