• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working with methods  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: I do not understand it at all. I would kindly ask someone to explain it step by step.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not understand what?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be that you don't understand it all and require step by step explanation.

Such things as class declaration or opening braces or closing you should understand as I see by your signature you have gained "Sun Certified Java Programmer" certificate. That is a bit misleading and I'm puzzled which parts we need to help you understand.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First it creates an object: Then it sets i to 10. After this line comes to play: Then: At this point my understanding ends. Please help.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us what answer you got after ran that and what you were expecting? And why?

This code snippet has few concepts involved, mainly: assignment, pass-by-value, class instantiation and instance variables

Hints for a start which should help you to think and disassemble problem yourself:
  • Line 17 is related with assignment concept
  • Lines 7, 8 with pass-by-value
  • Lines 20, 8 related with class instantiation and instance variables
  •  
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try taking paper and pencil and using that to keep track of what happens
    Have a row for the w variable and multiple columns representing each instance of the Wrapper class that is created
    The first column could be labeled: Created on line 15 and w's initial value would be 10. When line 16 is executed, the value of w changes to 20.
    Continue, Label the next column for when the next instance of Wrapper is created on line 8 called from line 17 and give its w a value of 10.
    etc
     
    Urs Waefler
    Ranch Hand
    Posts: 192
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Till now I have looked several hours on this code - no idea. I am stuck.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have looked several hours on this code
    Just looking is not enough, you must write it down.
    Did you try what I suggested - play computer with the program -  step through the statements one by one, writing down the results of each statement in columns on paper?

    No need for that actually.  Wait long enough and some one here will do it for you.


    A suggestion to make the code more readable and help with the understanding,
    rename the Wrapper class references from w to wrprRef to avoid a confusion between the int variable named w and the class reference named w.
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Urs Waefler wrote:Till now I have looked several hours on this code - no idea. I am stuck.

    Alright, I see. If I am not mistaken you are trying to get ready for certification, at least this kind of question looks like it is for that. If yes, have you read some study guide?

    More importantly, do you know how to use debugger which explains you everything in step by step process? They are integrated in every modern IDE, as well as in jdk command line tools package. Latter is a bit more difficult to use as requires to read some documentation first, but I have tried few times, and it wasn't that bad either.

    Line 15. Object of Wrapper class gets created. Instance variable w has value of 10.
    Line 16. Instance variable 'w' of Wrapper class's object (which is refered by 'w') gets reassigned value to 20. So now w = 20.
    Line 17. Method call, you pass object reference created at line 15, but at line 8 it gets reassigned to new object, hence, anything what is going to happen further not going to affect originally passed object unless returned value gets assigned to passed object's reference. Back to line 17. Returned result not assigned, meaning it gets discarded and no changes whatsoever to what we had at line 16. w = 20 still.
    Line 18. w = 20 so far and you add 30 to it. So w = 50 here of the object which is refered by same name reference variable 'w'.
    Line 19. Result printed 50.
    Line 20_1. It contains assignment operator. So differently from line 17, returned result will be reassigned to 'w' reference variable. within method at line 8 new object of Wrapper class gets created. w instance variable has value of 10 (as it is a new object). Again, to w instance variable you add 9, hence w = 19 (10 + 9). Lately created object reference gets returned.
    Line 20_2. Object reference from previuos step gets assigned to object initially created at line 15. So, forget what you had in between line 15 and 19 (including). Now you are dealing with other object.
    Line 21. You printing objects value, which was influenced by line 20_1 explanation. Hence printing latest w instance variabe value which was 19.

    So, you supposed to get in console printed:
    50
    19
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ???
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norm Radder wrote:???
    Certification questions are unique, so I think they best fit to our certification forum (this is where I'm going to move it), where answers nature slightly differs from the answers to the questions when OPs are trying to solve exercises (in BJ) where there is a need to come up with own solution.

    I don't think I crossed the line with giving away too much as you are trying to imply here Norm.
     
    Urs Waefler
    Ranch Hand
    Posts: 192
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Indeed the naming confuses me. The concepts behind are not difficult to understand; I must go through it again. Of course I tryed with paper and pencil; I did not come to a result. Probably because of the naming, you can see w twice.
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you saw this code while on a job, you would rename the variables.  If the two w's confuse you, try renaming one.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!