stupid question...waiting for a stupid answer

Discussion in 'Java' started by Brandon McCombs, Aug 22, 2006.

  1. I'm wondering why I can't do the following and get my desired result:
    //globals
    private hasPhoneNumberChanged = false;


    private void notifyChanges(boolean dirtyFlag) {
    dirtyFlag = true;
    setTitle("Object properties for: " + entryDN +
    " ---- Unsaved Changes");
    apply.setEnabled(true);
    }

    private class HomeOtherListener implements ActionListener {
    private JDialog owner = null;
    HomeOtherListener(JDialog d) {
    owner = d;
    }
    public void actionPerformed(ActionEvent e) {
    EditOtherAttributes o = new EditOtherAttributes(owner,true,
    otherHomePhone);
    o.setTitle("Home Number (Others)");
    if (!o.showEditOtherAttributesDialog())
    -----> notifyChanges(hasPhoneNumberChanged);
    }
    }


    If I print out the value of hasPhoneNumberChanged before and after where
    I call notifyChanges() the value of the boolean doesn't change. It only
    changes within notifyChanges(). It seems as if a copy of the boolean
    variable is being passed to notifyChanges() not a reference so that the
    variable local to notifyChanges() is modified instead of my global variable.

    Am I missing something? Should this work but in another form? Maybe I
    should use String instead of boolean to pass by reference?

    thanks
    Brandon
     
    Brandon McCombs, Aug 22, 2006
    #1
    1. Advertising

  2. Brandon McCombs

    Bjorn Abelli Guest

    "Brandon McCombs" wrote...

    The subject is "stupid question..."

    Well, it might not be "stupid", but the code you provided was somewhat
    incomprehensible...



    > I'm wondering why I can't do the following and get my desired result:
    > //globals
    > private hasPhoneNumberChanged = false;
    >
    >
    > private void notifyChanges(boolean dirtyFlag) {
    > dirtyFlag = true;
    > setTitle("Object properties for: " + entryDN +
    > " ---- Unsaved Changes");
    > apply.setEnabled(true);
    > }
    >
    > private class HomeOtherListener implements ActionListener {
    > private JDialog owner = null;
    > HomeOtherListener(JDialog d) {
    > owner = d;
    > }
    > public void actionPerformed(ActionEvent e) {
    > EditOtherAttributes o = new EditOtherAttributes(owner,true,
    > otherHomePhone);
    > o.setTitle("Home Number (Others)");
    > if (!o.showEditOtherAttributesDialog())
    > -----> notifyChanges(hasPhoneNumberChanged);
    > } }
    >
    >
    > If I print out the value of hasPhoneNumberChanged before
    > and after where I call notifyChanges() the value of the
    > boolean doesn't change. It only changes within notifyChanges().


    I can't even see that it changes there?

    Unless you believe that a boolean can be passed by reference, which it
    can't.

    Within notifyChanges() you only change the value of the local variable
    "dirtyFlag", unless it's already true, in which case you don't even change
    it.

    > It seems as if a copy of the boolean variable is being passed to
    > notifyChanges()
    > not a reference so that the variable local to notifyChanges()
    > is modified instead of my global variable.


    Yes.

    > Am I missing something?


    Probably that boolean is a primitive, not a class, which means that they can
    only be passed by value.

    Actually, objects can't be passed by reference either, even though they have
    some "pointer semantics". All arguments are passed by value, but the value
    of an "object argument" is the reference to the object.

    > Should this work but in another form? Maybe I should use String instead of
    > boolean to pass
    > by reference?


    Probably not, as you can't change the internal value of the String, as a
    String instance is immutable.

    As I'm not even sure what you want to accomplish, it's difficult to come up
    with some suggestions, but I'll give you some examples, which might explain
    how it works.

    ....

    class Holder
    {
    public boolean myValue = false;

    public Holder(boolean v)
    {
    myValue = v;
    }

    public String toString()
    {
    return Boolean.toString(myValue);
    }
    }

    ....

    /// In some other class...

    void called(int x, Holder y, Holder z)
    {
    x = 2;
    y = new Holder(true);
    z.myValue = true;
    }


    void calling()
    {
    int a = 1;
    Holder b = new Holder(false);
    Holder c = new Holder(false);

    called(a, b, c);

    // All arguments are passed by value, but that has
    // different meanings when dealing with either
    // primitives or object instances.

    // As a was an int, passed by value,
    // a copy of the direct value was passed into
    // variable x, which was replaced by the value 2.
    // The value of a remains the same.

    System.out.println(a); // 1


    // As a was an instance of Holder, the reference to that
    // instance was passed by value into variable y,
    // which was replaced by a reference to a new instance.
    // The value of b remains the same.

    System.out.println(b); // false

    // ...but it's possible to manipulate the *contents*
    // of an object to which you've received the reference to,
    // unless it's "immutable" like String.

    // Look into the method "calling" above, and see the
    // difference...

    System.out.println(c); // true
    }

    ....


    /// Bjorn A
     
    Bjorn Abelli, Aug 22, 2006
    #2
    1. Advertising

  3. Bjorn Abelli wrote:
    > "Brandon McCombs" wrote...
    >
    > The subject is "stupid question..."
    >
    > Well, it might not be "stupid", but the code you provided was somewhat
    > incomprehensible...
    >
    >
    >
    >> I'm wondering why I can't do the following and get my desired result:
    >> //globals
    >> private hasPhoneNumberChanged = false;
    >>
    >>
    >> private void notifyChanges(boolean dirtyFlag) {
    >> dirtyFlag = true;
    >> setTitle("Object properties for: " + entryDN +
    >> " ---- Unsaved Changes");
    >> apply.setEnabled(true);
    >> }
    >>
    >> private class HomeOtherListener implements ActionListener {
    >> private JDialog owner = null;
    >> HomeOtherListener(JDialog d) {
    >> owner = d;
    >> }
    >> public void actionPerformed(ActionEvent e) {
    >> EditOtherAttributes o = new EditOtherAttributes(owner,true,
    >> otherHomePhone);
    >> o.setTitle("Home Number (Others)");
    >> if (!o.showEditOtherAttributesDialog())
    >> -----> notifyChanges(hasPhoneNumberChanged);
    >> } }
    >>
    >>
    >> If I print out the value of hasPhoneNumberChanged before
    >> and after where I call notifyChanges() the value of the
    >> boolean doesn't change. It only changes within notifyChanges().

    >
    > I can't even see that it changes there?
    >
    > Unless you believe that a boolean can be passed by reference, which it
    > can't.
    >
    > Within notifyChanges() you only change the value of the local variable
    > "dirtyFlag", unless it's already true, in which case you don't even change
    > it.
    >
    >> It seems as if a copy of the boolean variable is being passed to
    >> notifyChanges()
    >> not a reference so that the variable local to notifyChanges()
    >> is modified instead of my global variable.

    >
    > Yes.
    >
    >> Am I missing something?

    >
    > Probably that boolean is a primitive, not a class, which means that they can
    > only be passed by value.
    >
    > Actually, objects can't be passed by reference either, even though they have
    > some "pointer semantics". All arguments are passed by value, but the value
    > of an "object argument" is the reference to the object.
    >
    >> Should this work but in another form? Maybe I should use String instead of
    >> boolean to pass
    >> by reference?

    >
    > Probably not, as you can't change the internal value of the String, as a
    > String instance is immutable.
    >
    > As I'm not even sure what you want to accomplish, it's difficult to come up
    > with some suggestions, but I'll give you some examples, which might explain
    > how it works.
    >
    > ...
    >
    > class Holder
    > {
    > public boolean myValue = false;
    >
    > public Holder(boolean v)
    > {
    > myValue = v;
    > }
    >
    > public String toString()
    > {
    > return Boolean.toString(myValue);
    > }
    > }
    >
    > ...
    >
    > /// In some other class...
    >
    > void called(int x, Holder y, Holder z)
    > {
    > x = 2;
    > y = new Holder(true);
    > z.myValue = true;
    > }
    >
    >
    > void calling()
    > {
    > int a = 1;
    > Holder b = new Holder(false);
    > Holder c = new Holder(false);
    >
    > called(a, b, c);
    >
    > // All arguments are passed by value, but that has
    > // different meanings when dealing with either
    > // primitives or object instances.
    >
    > // As a was an int, passed by value,
    > // a copy of the direct value was passed into
    > // variable x, which was replaced by the value 2.
    > // The value of a remains the same.
    >
    > System.out.println(a); // 1
    >
    >
    > // As a was an instance of Holder, the reference to that
    > // instance was passed by value into variable y,
    > // which was replaced by a reference to a new instance.
    > // The value of b remains the same.
    >
    > System.out.println(b); // false
    >
    > // ...but it's possible to manipulate the *contents*
    > // of an object to which you've received the reference to,
    > // unless it's "immutable" like String.
    >
    > // Look into the method "calling" above, and see the
    > // difference...
    >
    > System.out.println(c); // true
    > }
    >
    > ...
    >
    >
    > /// Bjorn A
    >
    >
    >


    OK, that answers my question. I'll just have to move the dirtyFlag =
    true; part to outside of the notifyChanges() method and set each boolean
    flag independently everytime I call notifyChanges().

    thanks
     
    Brandon McCombs, Aug 22, 2006
    #3
  4. Brandon McCombs wrote:
    [...]
    > Am I missing something? Should this work but in another form? Maybe I
    > should use String instead of boolean to pass by reference?


    Don't pass any parameter to notifyChanges() and use
    *this*.hasPhoneNumberChanged = false;


    :)
     
    Rohit Kumbhar, Aug 22, 2006
    #4
  5. Richard Wheeldon, Aug 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Günther Stößl

    Question: Waiting for multiple Objects

    Günther Stößl, Mar 13, 2006, in forum: Java
    Replies:
    4
    Views:
    4,915
    Chris Uppal
    Mar 14, 2006
  2. Replies:
    14
    Views:
    1,522
    Remon van Vliet
    Apr 6, 2006
  3. QDL
    Replies:
    0
    Views:
    319
  4. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,023
  5. Daedalus
    Replies:
    14
    Views:
    178
    Daedalus
    Jun 25, 2004
Loading...

Share This Page