• 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

pass by reference???

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

We say that in Java language we pass by value only, look into the snippet below:


public class PassByRef
{
int i=1;
int j=2;

public void change(PassByRef obj)
{
obj.i=obj.i*10;
obj.j=obj.j*10;
}
public static void main(String a[])
{
PassByRef obj1= new PassByRef();
obj1.change(obj1);
System.out.println("*****obj1.i is **"+obj1.i);
System.out.println("*****obj1.j is **"+obj1.j);
}
}

Here we are passing the reference of the object as arguments to the method when method is called.

Thanks & Regards,

amit
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see a question here. Are you asking something, or simply making an observation?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method change() can alter the state of the object currently referred to by local variable obj1 but change() cannot alter obj1 to refer to a different object. What is being passed by value is not an object but a reference variable.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, the reference is passed by value.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this helps:
http://www.xdweb.net/~dibblego/java/faq/answers.html#q21
reply
    Bookmark Topic Watch Topic
  • New Topic