Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > stupid question...waiting for a stupid answer

Reply
Thread Tools

stupid question...waiting for a stupid answer

 
 
Brandon McCombs
Guest
Posts: n/a
 
      08-21-2006
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




 
Reply With Quote
 
 
 
 
Bjorn Abelli
Guest
Posts: n/a
 
      08-22-2006

"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



 
Reply With Quote
 
 
 
 
Brandon McCombs
Guest
Posts: n/a
 
      08-22-2006
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
 
Reply With Quote
 
Rohit Kumbhar
Guest
Posts: n/a
 
      08-22-2006
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;



 
Reply With Quote
 
Richard Wheeldon
Guest
Posts: n/a
 
      08-28-2006
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?


Java doesn't do pass by reference:

http://www.javaworld.com/javaworld/j...0526-pass.html

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 8 06-27-2006 01:51 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
[Newbie] Stupid problem need simple answer (Array & RegExp) Daedalus Perl Misc 14 06-24-2004 11:47 PM



Advertisments