Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help

 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String args[]) {
Qcb90 obj = new Qcb90();
obj.f();
}
}

output is 1 0 1
but I was expecting 0 0 1
why is that ??
can anybody explain it?
Thanx
Jaffery
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the variable a is not passed to the method g, the assignment to a in that method effects the instance member variable, which is accessible in method f. That's why you see the first 1 when it is printed.
I hope that helps,
Corey
 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hhmm...
need little bit more clue......if it is possible
why c is 1 and b is 0
Thanx,
Jaffrey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic