aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help" Watch "need help" New topic
Author

need help

Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
hhmm...
need little bit more clue......if it is possible
why c is 1 and b is 0
Thanx,
Jaffrey
 
Don't get me started about those stupid light bulbs.
 
subject: need help
 
Similar Threads
Another program. . .
Reg. Reference datatypes in method calls
Question from khalids sample paper
Method calls
is this strange