• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Pass by value vs. Pass by reference

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am confused with this piece of program:
01 ublic static void main(String[] args) {
02: A a = new A();
03: a.setStatus("5");
04: changeStatus(a);
05: System.out.println(a.getStatus());
06: }
07:
08: public static void changeStatus(A a) {
09: a = new A();
10: a.setStatus("10");
11: }

what is the output? Can anybody explain me why that is happening? I thought that after the changeStatus(a) function called reference a will point to a new object...

Thank you in advance.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mukhan,

Welcome to the JavaRanch. In the future, Use Code Tags to make your code more readable so we can help you more easily.

What's happening when you call changeStatus(A a) is that a copy of the reference to A a in your main method is passed to changeStatus. When you create a new object in changeStatus, it's that copied reference that is changed, not the original reference in main. Hence, when the method returns, the change to the new object is lost, and the value returned by getStatus is not changed.

This JavaWorld article gives a nice explanation with good images that explains in more detail.
 
mukhan myrzakulov
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man. I appreciate your help.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Also see Pass-By-Value Please.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of answers to questions like this depend on whether the object was passed by value or by reference. Once you know to differentiate that these questions should be a breeze
 
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

Originally posted by John Meyers:
The output of answers to questions like this depend on whether the object was passed by value or by reference. Once you know to differentiate that these questions should be a breeze


In Java, variables of non-primitive types are references to objects. They are not the objects themselves. Because variables are not the objects themselves, objects are never passed in Java. Only references to objects are passed.

In Java, everything is always passed by value. Primitives are passed by value, and object references are passed by value.

So you'll never need to differentiate between pass-by-value and pass-by-reference - you just need to be aware that variables are references, and not objects.
[ May 17, 2008: Message edited by: Jesper Young ]
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:

In Java, variables of non-primitive types are references to objects. They are not the objects themselves. Because variables are not the objects themselves, objects are never passed in Java. Only references to objects are passed.

In Java, everything is always passed by value. Primitives are passed by value, and object references are passed by value.

So you'll never need to differentiate between pass-by-value and pass-by-reference - you just need to be aware that variables are references, and not objects.

[ May 17, 2008: Message edited by: Jesper Young ]



whoops ! my wording was wrong there. I meant to point out the difference between primitives and objects. Here is a better illustration. Thank you for pointing out the error



Starting from method test()
[ May 17, 2008: Message edited by: John Meyers ]
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic