Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

doubt in parameter passing

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haii i am preparing for scjp 1.6.am new to this forum i have a doubt in parameter passing plzz help.

public class Customer
{
public static void main(String args[])
{
Pizza favouritePizza = new Pizza();
System.out.println("Meat befor baking : " +favouritePizza.meat);
bake(favouritePizza);
System.out.println("Meat on Pizza after Baking : "+ favouritePizza.meat);
}

staic void bake(Pizza pizzaToBeBaked)
{
pizzaToBeBaked.meat = "Chicken";

pizzaToBeBaked = null; // (1)
}

}

class Pizza
{
String meat = "Beef";
}




My doubt is even after setting pizzaToBeBaked = null; /at (1) there is no change for pizzaToBeBaked.meat


plzz help.i have just started learning java. thanks in advance
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

toneykorah coder wrote:plzz help.i have just started learning java. thanks in advance



I'll give it a shot! This is a common question I think. Here's what's happening:

Java passes values into methods by passing in a _copy_ of the primitive value (in the case of int, double, etc) or a _copy_ of the reference to the object (in the case of objects, like your Pizza). Therefore, inside the bake(...) method, you can still reach the object favouritePizza itself because both the copy ("pizzaToBeBaked") and the original reference ("favouritePizza") both point to the exact same object (on the heap). Think of the object reference as an "address" to where the object is.

However, since the local variable pizzaToBeBaked is only a COPY of the reference, once you do something with the reference itself, it doesn't affect the original anymore. You can even create an entirely new pizza in there and point your "pizzaToBeBaked" reference to it (ie "pizzaToBeBaked = new Pizza();") and that pizza will live and die with the local method (the scope of the pizzaToBeBaked variable is only within the method).

So when you say that pizzaToBeBaked = null, you're only saying that "my local copy of this object reference is no longer going to point to my original pizza". In your main method, the original reference still points to the original pizza (which you modified in the bake method by directly altering its meat field), so when you print it out it gives you the contents of that original object, including any modifications.

Was that understandable?
 
tonie korah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
 
Greenhorn
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm studying this chapter as well, but correct me if I'm wrong.

Oyvind was correct, the pizzaToBeBaked parameter in the bake() method points to the same object in the heap, however if in the local method, any changes to the state of the pizzaToBeBaked object, will reflect to the favouritePizza object in the main method.

However if the pizzaToBeBaked object is refer to a new Pizza or null, it will not be reflected on the favouritePizza object.

Did I get the concept right?

Cheers
Alvin
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toreycorah coder - please check your private messages for an administrative message from JavaRanch.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right Kelvin.
To make this story even shorter i will say only two things.
First off all you can think that pizzaToBeBaked reference holds just an address of Pizza object in memory nothing more


However if the pizzaToBeBaked object is refer to a new Pizza or null, it will not be reflected on the favouritePizza object.


And pizzaToBeBaked is not an object it is just a reference.

Dejan.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic