Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Passing object reference variables

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I have come across a program in JavaBeat Mock exams which I have quoted below. As per K&B text book(Pg 203), passing of object reference variables to a method that modifies the object state will modify the original state of the object. But the program below is giving a different output. Take a look...



The output I was expecting was "java.awt.Button null" while the program outputs "null java.awt.Button".
Can anyone explain me the program's behavior?
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,
you are calling test2 method by passing button1's value.Inside the test2 method, the button reference variable points to a new Button object created inside the method.But button1 reference variable inside the main method still points null.
Similarly, you are calling test1 method by passing button2 's value.Inside the test1 method the button reference variable points to null, but still button2 variable in main method points to the created button object in the main.
Understand that in test1 and test2 the button reference variables do not change the value of the object passed rather they point to a null and newly created object respectively.
To understand it better try to print the hashcode values of the button reference variables.
hope this helps.

thanks,
Raja
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

remember, when you pass a Object reference, you are passing a COPY of the reference. So, if you change the value of the REFERENCE (not the Object of the reference) inside the method you'll be changing only the value of the reference inside the method. Got it? Let's see an example:



It will show:



For the Object, any reference with the same Object will have the same value for it. Now see two reference's that have like a "pointer" for the same object, but, when we call a method, one of them loose your reference for the Object.

Look this example and see if it will make the things clear:



it will show:



I hope it helps.

Raphael Rabadan
[ July 12, 2008: Message edited by: Raphael Rabadan ]
 
Forget Steve. Look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic