wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid Mock Q - Hide & Seek ??!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid Mock Q - Hide & Seek ??!!" Watch "Khalid Mock Q - Hide & Seek ??!!" New topic

Khalid Mock Q - Hide & Seek ??!!

S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
class Exthis
int a;
int b;
public void f()
int[] c = {0};
System.out.println(a+" "+b+" "+c[0]);
public void g(int b, int[] c)
public static void main(String[] args)
Exthis obj = new Exthis() {};
o/p : 1 0 1
can somebody please reason why b is 0 while c[0] has changed to 1. pointers to explanation in JLS will be appreciated.
kamal jaisingh
Ranch Hand

Joined: Feb 23, 2001
Posts: 39
the value of b is not changed cause u r passing copy of a variable b.
and b is of primitive datatype , so the changes taken place to b inside the method does not affect the value of b outside the method since only copy of b is sent and not b itself.
as for c changing, in this case also copy of c is sent to the method, but since c is an array , tht nmeans its an object, so the value of c changes , java works with references and not directly with objects.

rule , when u pass an object as an argument , u can change the value, ie, reference of tht object in method body ,
when u pass primitive type, no changes r made.
u can make changes to a variable only if u pass an object .
in ur case c is an object ..
hope this helps
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
i thought so too.
if i change line # 11 to:
public void g(int k, int[] c)
i get the o/p as :
1 1 1
why does b get modified?
[This message has been edited by Sweekriti Engineer (edited April 03, 2001).]
[This message has been edited by Sweekriti Engineer (edited April 03, 2001).]
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
in your method g(b,c)
b and c are local to the method void g(b,c).
your System.out.println() is outside the method g(b,c).so a,b should be the instance variable of the class.please correct me.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Sweekriti,
In your first example you are hiding the class variable b with the local variable of the same name. Therefore you are only altering the local variable inside the method g.
In your second example, changing line #11, you are changing the first parameter being passed so that it is no longer hiding the class variable b. Since b is a member variable your method g can change it to anything it wants to.
Kamal is correct in what the Java rules are for passing parameters to methods (references can't be changed but contents can). However if you have a non-static method you can change any member variable (assuming you are not hiding it!).
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
yes it makes sense. thanks everyone.
is this actually "hiding" or is it "shadowing" or "obscuring" ?
what's the major difference between these 3?
I agree. Here's the link: http://aspose.com/file-tools
subject: Khalid Mock Q - Hide & Seek ??!!
It's not a secret anymore!