This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help" Watch "need help" New topic

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();

output is 1 0 1
but I was expecting 0 0 1
why is that ??
can anybody explain it?
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,

SCJP Tipline, etc.
Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
need little bit more clue......if it is possible
why c is 1 and b is 0
I agree. Here's the link:
subject: need help
Similar Threads
Method calls
Reg. Reference datatypes in method calls
Question from khalids sample paper
Another program. . .
is this strange