Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

stupid question...waiting for a stupid answer

Thread view: 
Brandon McCombs - 22 Aug 2006 00:33 GMT
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
Bjorn Abelli - 22 Aug 2006 01:13 GMT
"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
[quoted text clipped - 23 lines]
> 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
Brandon McCombs - 22 Aug 2006 02:53 GMT
> "Brandon McCombs" wrote...
>
[quoted text clipped - 134 lines]
>
> /// 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
Rohit Kumbhar - 22 Aug 2006 03:07 GMT
[...]
> 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;

:)
Richard Wheeldon - 28 Aug 2006 19:57 GMT
> Am I missing something? Should this work but in another form? Maybe I
> should use String instead of boolean to pass by reference?

Java doesn't do pass by reference:

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.