Passing by reference, more confusion...

H

harry

I have an object that's passed in to a function as a parameter i.e public
boolean getProjectTitle(ProjectHeader_DTO obj) {...}

If I then call a method on this object inside the function i.e
obj.setTitle("test") then using obj.getTitle() from outside the function it
displays the property set correctly!

But doing something like this inside the function doesn't -

ProjectHeader_DTO x = new ProjectHeader_DTO();
x.setProjectTitle("test");
obj=x;

As long as I explictly set the properties it works but why not when
assigning another object?

Tried using the clone() method aswell & inside the function its works fine
until after the return!

I do not want the object to be return'ed from the function!

Any ideas?

thanks

harry
 
J

joey tribbiani

harry said:
I have an object that's passed in to a function as a parameter i.e public
boolean getProjectTitle(ProjectHeader_DTO obj) {...}

If I then call a method on this object inside the function i.e
obj.setTitle("test") then using obj.getTitle() from outside the function it
displays the property set correctly!

But doing something like this inside the function doesn't -

ProjectHeader_DTO x = new ProjectHeader_DTO();
x.setProjectTitle("test");
obj=x;

As long as I explictly set the properties it works but why not when
assigning another object?

When you call a function, the parameters are copied and only a copy of
the varible is used inside the function ( this is why passing int x does
not change x outside the function )
When you pass an object, not the object itself is passed but a pointer
to the memory of the object, the pointer is copied, but still shows to
the same memory. So obj.setTitle("test") inside the function changes the
title from the object obj points to ( and this is the same mermory, obj
outside the function points to ). When you create a new object x, x
points to some other memory. The statement obj=x says: make obj point to
where x points to ( to the newly allocated memory ). Assuming x was
created inside the function, x gets destroyed, as soon as you leave the
function. After the function, obj still points to the old obj-memory,
because the obj you assigned to x is only a copy of obj

think of it as houses and addresses: You have a green house ( you need
memory (space) for your house ) and you have a piece of paper with its
address: 5th avenue. If you copy or change the address on the paper, you
don't change anything about your house.
consider following pseudo-code:

void clean_house ( House green ) // passing copy of your paper with address
{
green.make_house_nice_and_clean(); // address points to the green house
}
-> green house is clean now!

void clean_house ( House green)
{
House red = new House; // building a new house somewhere else
green = red; // Address now indicates the red house
green.make_house_nice_and_clean(); // cleaning the red house!!!!

}
-> green house is still dirty

If you tell somebody to clean your house, it is impossible, to "give"
him the house, you rather leave the address - same with java: coping and
moving around all the mermory for the house is much less efficent than
passing the address to this mermory.

Hope this was helpful for you, if not, read your java Textbook again ;)
 
T

Timo Kinnunen

I have an object that's passed in to a function as a parameter i.e public
boolean getProjectTitle(ProjectHeader_DTO obj) {...}

If I then call a method on this object inside the function i.e
obj.setTitle("test") then using obj.getTitle() from outside the function it
displays the property set correctly!

But doing something like this inside the function doesn't -

ProjectHeader_DTO x = new ProjectHeader_DTO();
x.setProjectTitle("test");
obj=x;

As long as I explictly set the properties it works but why not when
assigning another object?

Because you haven't defined and called any method to do the assigning. Java
doesn't do that for you.
 
J

Joona I Palaste

Because you haven't defined and called any method to do the assigning. Java
doesn't do that for you.

Are you again starting with the conclusion that Java has pass by
reference and twisting definitions to try to arrive at that conclusion?
 
T

Tony Morris

harry said:
I have an object that's passed in to a function as a parameter i.e public
boolean getProjectTitle(ProjectHeader_DTO obj) {...}

If I then call a method on this object inside the function i.e
obj.setTitle("test") then using obj.getTitle() from outside the function it
displays the property set correctly!

But doing something like this inside the function doesn't -

ProjectHeader_DTO x = new ProjectHeader_DTO();
x.setProjectTitle("test");
obj=x;

As long as I explictly set the properties it works but why not when
assigning another object?

Tried using the clone() method aswell & inside the function its works fine
until after the return!

I do not want the object to be return'ed from the function!

Any ideas?

thanks

harry

Here are a few fundamental concepts that you seem to misunderstand:
a) Java does not have functions, it has methods
b) Java does not allow you to declare types that are objects, only object
references, so you could not possibly be passing an object or returning one
from a function [sic]
c) Java is strictly pass by value, there is no pass by reference in Java

http://www.xdweb.net/~dibblego/java/faq/answers.html#q21

Good luck.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Roedy Green

Here are a few fundamental concepts that you seem to misunderstand:
a) Java does not have functions, it has methods
b) Java does not allow you to declare types that are objects, only object
references, so you could not possibly be passing an object or returning one
from a function [sic]
c) Java is strictly pass by value, there is no pass by reference in Java

there is no comp.lang.java newsgroup.

see http://mindprod.com/jgloss/callbyvalue.html
and http://mindprod.com/jgloss/callbyreference.html
to get this all clear.
 
T

Timo Kinnunen

Are you again starting with the conclusion that Java has pass by
reference and twisting definitions to try to arrive at that conclusion?

No, I'm answering the OP's question.
 
J

Joona I Palaste

No, I'm answering the OP's question.

In a very curious way. In Java, assigning is only done with the =
operator. You don't "define methods to do the assigning".
 
M

Michael Borgwardt

Joona said:
In a very curious way. In Java, assigning is only done with the =
operator. You don't "define methods to do the assigning".

Isn't that exactly what set methods do?
 
J

Joona I Palaste

Isn't that exactly what set methods do?

Set methods use the = operator to actually assign the value. I'm not
sure whether Timo meant such set methods or methods that "assign"
something by only calling other methods.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
T

Timo Kinnunen

In a very curious way. In Java, assigning is only done with the =
operator. You don't "define methods to do the assigning".

There is no assigning to an object in Java, which it seemed the OP was
looking for. So, short of redesigning his code, the OP needs to provide and
activate such an abstraction himself. To do that, a method "to do the
assigning" fits the bill well.
 
J

Joona I Palaste

There is no assigning to an object in Java, which it seemed the OP was
looking for. So, short of redesigning his code, the OP needs to provide and
activate such an abstraction himself. To do that, a method "to do the
assigning" fits the bill well.

Design-wise, this might be a good approach. But it won't be "assigning"
anything, at least not to the variable given as the method parameter.
To avoid the equivocation fallacy, I suggest a better name for the new
method.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
T

Timo Kinnunen

Design-wise, this might be a good approach. But it won't be "assigning"
anything, at least not to the variable given as the method parameter.
To avoid the equivocation fallacy, I suggest a better name for the new
method.

But I didn't suggest any method name, I just described the abstraction :)
 
R

Roedy Green

Isn't that exactly what set methods do?

Indirectly. The actual assignment is done inside the method with =.

there is nothing magic about a set method other than the beanbox
exposes them to the public.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top