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

Interesting question on object reference variables

 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As part of my studies I am taking an online course targeting certification but this is a pretty elementary question so I put it here.
On the subject of instantiating objects....they wanted to show an example of how ONE LINE could 1)declare a variable, 2) instantiate and initialize and object of a certain type, 3) call a method of that class, and finally 4) return the result back to the initial variable.

Here is the line (changed to make it mine....):

String name = new Account("Bob Ruth","1234 Any Street").getName();

for now, assume that the class is properly declared.... I just grabbed this one line. The line itself I can understand clear as a bell. That's not what I am curious about.

I personally see no explicit reference variable created to hold the reference to the new object instantiated. My little "pea brain" makes me think that the object is instantiated, initialized, and lives long enough to regurgitate the name back to the "name" variable, and would then be completely abandoned and left as garbage collection fodder. I can live with it if that IS the case, I just want to make sure that I am not missing something in the mechanics here.

Thanks a million,
Bob
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Yep, that's the case; unless the Account object does something in its constructor or getName() method to store a reference to itself, somewhere, then it's not referenced and can be immediately collected.
 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest. I kind of thought that was it.

I realize that I can go prototype this as well as the next guy but, just for thought, would this work?

Account myAccount;
String name = (myAccount = new Account("Bob Ruth","1234 Any Street")).getName();

my thinking here is that doing a myAccount = and enclosing that in a new set of parens, that the new object ref would get put in myAccount.... AND that the (myAccount = new Account(yadda yadda yadda)) would STILL evaluate to the new object reference so the .getName should still work..... I think.....

by the way.... the mentor at the training site emailed me back and said the same thing that I thought and that you said. I must not be as dumb as I look!!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you could do that, but now we're getting really, really far into the realm of Ugly and Confusing Code. It's no more efficient to do this all in one line, but it sure is a lot harder to read!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'd say if you're going to do that, to just do this:

 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldnt agree with you more .... I dont code in that style of jamming it all in as few lines as possible... I'd rather have it readable because I may be the slob back in here reading it again 2 or 3 years from now!!!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can delete your own double posts. when you go to edit it, there's a checkbox near the topleft for "delete post".

be warned, if yours is the first post in the thread and you delete it, the whole thing goes away forever.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic