stupid question...waiting for a stupid answer

B

Brandon McCombs

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
 
B

Bjorn Abelli

...

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
 
B

Brandon McCombs

Bjorn said:
...

The subject is "stupid question..."

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




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.


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.


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
 
R

Rohit Kumbhar

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;


:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top