-3

I am new to java. I tried to search a lot for my query but could not find. Please help me if you know. I have a function:

boolean func(int a, int b, myclass obj1, myclass2 obj2)
{
    ...
}

   void caller() {
   int a = 0, b=0;
   myclass obj1 = null;
   myclass1 obj2 = null;
   func(a,b,obj1,obj2);
   if (a == 5 && b ==2)
  {
   ...
 }
}

what should i do such that all passed variables have the value in caller function which was given by function func?

user967491
  • 121
  • 1
  • 7
  • 1
    What do you mean, by "changes are made in the called function"? – NickLH Nov 03 '11 at 16:17
  • I don't understand what you mean by "Is there any means by which changes are made in the called function also?". Also Java has no "functions" but "methods". Anyway in Java objects are passed as pointers by value and there's no way to change that. – m0skit0 Nov 03 '11 at 16:17
  • 1
    possible duplicate of [Is Java pass by reference?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference). Just look at all the related questions, at the right of the page. – JB Nizet Nov 03 '11 at 16:17
  • http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html – Brian Roach Nov 03 '11 at 16:18

5 Answers5

3

In Java everything is passed by value, there's no exception.

If you pass references to objects (which every non-primitive variable is) those references are copied as well. You can then assign new objects to those parameters but those changes are not reflected in the calling method.

If you change the passed object, however, (e.g. change an attribute of that object) you'll see those changes in the calling method as well.

Example:

class Person {
  public String name; //note that public here is generally bad practice but it's here for simplicity's sake
}

public void someMethod(Person p) {
  p.name = "Bob"; //this will be visible to the caller since you change the object
  p = new Person(); //this won't be visible to the caller since you change the reference     
}
Thomas
  • 87,414
  • 12
  • 119
  • 157
1

Java is a pass by value language. There is no way to pass a variable by reference. However, when you pass an object (as opposed to a primitive), the reference is what gets passed by value.

Since int a and int b are primitives, any changes you make to them will not be reflected outside the scope of the method. However, the changes you make to myclass obj1 and myclass2 obj2 WILL be reflected outside the method because they are objects.

Edit: As Brian and Matteo have pointed out, if you reassign obj1 or obj2 to a new instance of myclass this will NOT take effect outside the method. Only mutations of the original reference will be persisted. This is because the reference to the original object is what got passed to the method, not a reference to the original variables obj1 and obj2.

dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • You can *use the methods* of `obj1` and `obj2`. – Brian Roach Nov 03 '11 at 16:21
  • 1
    that's not complete. changes you make on the original obj1 and obj2 will be reflected outside. However, if you reassign obj1 or obj2, outside you will not see any change. – Matteo Nov 03 '11 at 16:25
  • @Matteo: Yes that is correct. I'll change my answer to be a bit more explicit on that point. – dbyrne Nov 03 '11 at 16:27
  • @dbyrne I'm just trying to prevent some headaches... why that re-assignment is not reflected outside? :-) – Matteo Nov 03 '11 at 16:29
1

in Java arguments are passed by copy (both primitive types and object references), so the following code does not have any effect on the caller

void func(int a, Object o) {
  a = 0;
  o = null;
}

int i = 10;
Object p = "hello";
func(i, p);
// i is still 10, p is "hello"

However, the referred object is the same, and you can update it... so this code do HAVE effects visible to the caller:

class Person {
  String name;
}

void func(Person p) {
  p.name = "myName";
}

p.name = "yourName";
func(p);
// p.name is "myName"
Matteo
  • 1,367
  • 7
  • 25
0

In the called function obj1 and obj2 will be also changed. Integer values a and b will not be changed.

And the function will deliver boolean as result.

Kayser
  • 6,544
  • 19
  • 53
  • 86
0

Java passes method arguments by value, not by reference. a can be modified because they are not declared final but the values will not be changed in the caller.

Terry Gardner
  • 10,957
  • 2
  • 28
  • 38