• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling and Object's Methods

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic