File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid Mock Q - Hide & Seek ??!!

 
S Dave
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Exthis
{
int a;
int b;
public void f()
{
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)
{
Exthis obj = new Exthis() {};
obj.f();
}
}
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Kamal
 
S Dave
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!).
Regards,
Manfred.
 
S Dave
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it makes sense. thanks everyone.
is this actually "hiding" or is it "shadowing" or "obscuring" ?
what's the major difference between these 3?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic