• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling and Object's Methods  RSS feed

 
Razia Sultana
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through Learning the java language Trail from official java tutorials. The below two topics teaches to use objects
Referencing an Object's Fields
and
Calling an Object's Methods

In the first topic the below code doesn't keep the reference to the object that got created.
int height = new Rectangle().height;

The explanation given for the above code is below:

"After this statement has been executed, the program no longer
has a reference to the created Rectangle, because the program never stored the reference anywhere."

Below is the example of second topic:

int areaOfRectangle = new Rectangle(100, 50).getArea();

In the above sentence does the object reference gets stored somewhere and why?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no reference to the created rectangle is save. that Rectangle object gets created, you call its getArea method, that value gets returned and saved in your areaOfRectangle variable, and then the object becomes eligible for garbage collection.

The answer to "why isn't it saved" is because the programmer didn't bother to save it.

If you think is is saved, WHERE would it be saved?
 
Razia Sultana
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!