• 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 Methods on Objects

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi;)

Since I am doing last checks for my Exam,
I stumbled upon this exam question, which I
do not completely understand:



It asks what the Code outputs. It outputs "false."

As an Explanation it states: "Since Java is pass by value, reassignments to method parameters are not seen by the caller."

I understand what that means, and I see, that primitives get "forgotten about" what they are not reassigned or saved
in a variable after the Method end, but I thought it's not the same with Objects...I thought that they still exist
with a changed state after they method (if the method actually changes the state...).

What I am especially interested of is what happens here:




When I pass in the toy5 Object, and this statements happens:



does the toy5 Reference (the one which was created with Toy toy5 = new Toy();)
now point to a new Toy Object in memory, and the original Object gets garbage collected?
Or does toy5 point to two Objects in memory (which I don't think)?

I hope someone can explain to me what happens there (as this confuses me the most):

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
When I pass in the toy5 Object, and this statements happens:



does the toy5 Reference (the one which was created with Toy toy5 = new Toy();)
now point to a new Toy Object in memory, and the original Object gets garbage collected?
Or does toy5 point to two Objects in memory (which I don't think)?



Neither. The toy5 reference variable, declared in the main() method, remains unchanged. It is still pointing to the object that was passed into the method.

Henry
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neither. The toy5 reference variable, declared in the main() method, remains unchanged. It is still pointing to the object that was passed into the method.



I see. And what exactly happens here?



Kind regards;)
Florian
 
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
I see. And what exactly happens here?




Here you have a new local reference variable toy and it starts pointing to a new Toy object, and on which the method removeIce() is called.

Now, if you wouldn't create a new object here, but would call the removeIce() on this local reference variable toy, it would change the state of the object, created in the main method:
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:I understand what that means, and I see, that primitives get "forgotten about" what they are not reassigned or saved
in a variable after the Method end, but I thought it's not the same with Objects...I thought that they still exist
with a changed state after they method (if the method actually changes the state...).


This excellent article is a must-read! In this excellent topic you'll find a great explanation (with some code examples) about pass-by-value with reference variables. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic