File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes a problem with the resolt of the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a problem with the resolt of the code " Watch "a problem with the resolt of the code " New topic
Author

a problem with the resolt of the code

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
this is how i think this code should work
i put it also in the debugger
and it also shows the resolt 11 2 7

and then when it goes to the print method suddenly it shows
a=7 b=2 c=7

why??

i showed here the diagram which represents my logic

http://img401.imageshack.us/my.php?image=img8298gp7.jpg

where did i go wrong??

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I hope you never hand in such convoluted code for an examination.

You are setting the parameter a equal to 11 in the two-arguments constructor, not the field, which would be this.a
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

First of all, this code should not compile. Class A doesn't implement interface C. But, assuming that you posted the wrong code, and everything else is correct...

this is how i think this code should work
i put it also in the debugger
and it also shows the resolt 11 2 7


Well, I highly doubt that... There are no timing issues (shown in the code) related to this application that would make an actual run different from a debugging run.

and then when it goes to the print method suddenly it shows
a=7 b=2 c=7

why??


Well, 7 2 7 is correct. And here is why...



In step 6 of your diagram, you have this line as assigning the value of the parameter b to the instance variable a. This is not true. This line assigns the value of the parameter b to the parameter variable a. To do what you want expect in step 6, you need to do this...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i forgot to mention that the first part of the question was
to fix the code

i added abstract in the the signature of class A

is that changes some how your answers??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Originally posted by donaldth smithts:

is that changes some how your answers??


That explains why it compiles. None of the other answers changes.

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
so you are saying that in step 6

that i changed the input variable "a"(a variable which exists only in the signature)??
and not this.a??

my next question (presumption) is that in step 5 i made a similar
command
but here b.a do change although its a=b too
because the signature is public B(int b)
there is no a there so we change the b.a variable
am i correct
[ February 17, 2008: Message edited by: donaldth smithts ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by donaldth smithts:
so you are saying . . .
my next question (presumption) is that in step 5 i made a similar
command
but here b.a do change although its a=b too
because the signature is public B(int b)
there is no a there so we change the b.a variable
am i correct?


Yes.
 
 
subject: a problem with the resolt of the code