BlueJ learner quick question on java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jay89
    New Member
    • Nov 2007
    • 3

    #1

    BlueJ learner quick question on java

    Hi!
    Having studied a bit of programming years back (bit of Basic and Pascal) I\'m currently trying to learn Java using BlueJ and don\'t quite understand what is going on in one particular situation! My previous programming experience leads me to expect a totally different answer!

    The problem is that I have some code that looks like this:

    [CODE=Java]

    Bird beaky = new Bird ();
    beaky.setWingsp an (4);
    Bird puffy = beaky;
    beaky.setWingsp an (6);

    [/CODE]

    All the classes and methods are set up and the program executes fine but when I poll the variables after executing I would have expected puffy\'s wingspan to be 4 and beaky\'s to be 6 - in fact they both have a wingspan of 6. Can anyone explain to me why this is?

    Thank you in advance for your help! It\'s a silly little question but it\'s got me totally flummexed!

    Jay
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by jay89
    Hi!
    Having studied a bit of programming years back (bit of Basic and Pascal) I\'m currently trying to learn Java using BlueJ and don\'t quite understand what is going on in one particular situation! My previous programming experience leads me to expect a totally different answer!

    The problem is that I have some code that looks like this:

    [CODE=Java]

    Bird beaky = new Bird ();
    beaky.setWingsp an (4);
    Bird puffy = beaky;
    beaky.setWingsp an (6);

    [/CODE]

    All the classes and methods are set up and the program executes fine but when I poll the variables after executing I would have expected puffy\'s wingspan to be 4 and beaky\'s to be 6 - in fact they both have a wingspan of 6. Can anyone explain to me why this is?

    Thank you in advance for your help! It\'s a silly little question but it\'s got me totally flummexed!

    Jay
    Think of 'beaky' and 'puffy' being pointers to a bird or birds. Your program fragment
    shows that both beaky and puffy point to the same single bird. So if you change
    properties of that bird (such as the wing span) both beaky and puffy notice that
    change just because they both point to that same bird.

    kind regards,

    Jos

    Comment

    • jay89
      New Member
      • Nov 2007
      • 3

      #3
      Thank you very much, Jos! That clears that up! So does that mean that beaky and puffy are essentially the same object and that that object has 2 names?
      Or am I just confusing myself?!! (Not as difficult as I previously thought, it appears!)
      Thanks again for your reply.
      Jay

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jay89
        Thank you very much, Jos! That clears that up! So does that mean that beaky and puffy are essentially the same object and that that object has 2 names?
        Or am I just confusing myself?!! (Not as difficult as I previously thought, it appears!)
        Thanks again for your reply.
        Jay
        Every variable or parameter in Java that is not a primitive (int, long, byte etc.)
        is actually just a pointer to an object. Basically the only way you can create
        an object is by using the 'new' operator. One single object per one single 'new'
        in your code; if you use the 'new' operator in a loop, of course you have that
        many objects created by that 'new' operator. If you do 'foo= bar' then both foo
        and bar point to the same object. Java is a simple language.

        kind regards,

        Jos

        Comment

        • jay89
          New Member
          • Nov 2007
          • 3

          #5
          Once again, thank you Jos (think I accidentally pressed report instead of reply just now - sorry mods!) for a concise and speedy reply! That has cleared up that little muddle and should keep me going for the next week or so (she says!). I\\\'m really grateful that there are people like you happy and willing to take the time and effort to help others - especially in this day and age! Please know that everything that you do is much appreciated!
          Thank you again
          Jay

          Comment

          Working...