By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Passing by reference, more confusion...

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
harry wrote:
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 ;)

Jul 17 '05 #2

P: n/a
oh, and in Java there is no such thing as passing by reference
Jul 17 '05 #3

P: n/a
Read this article on javaworld about pass-by-ref
http://www.javaworld.com/javaworld/j...0526-pass.html

If your question still remains unanswered OR you are still confused -
post a similar cheat sheet (as done in the article -so that we can get
you to understand it !)

Arvind
"harry" <a@abc.com> wrote in message news:<bt*********************@news-text.cableinet.net>...
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

Jul 17 '05 #4

P: n/a
see http://mindprod.com/jgloss/callbyvalue.html
http://mindprod.com/jgloss/callbyreference.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #5

P: n/a
On Sun, 09 May 2004 14:44:23 GMT, harry wrote:
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.
Jul 17 '05 #6

P: n/a
Timo Kinnunen <ti***********@bigfoot.com> scribbled the following:
On Sun, 09 May 2004 14:44:23 GMT, harry wrote:
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.


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Jul 17 '05 #7

P: n/a
"harry" <a@abc.com> wrote in message
news:bt*********************@news-text.cableinet.net...
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/...swers.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

Jul 17 '05 #8

P: n/a
On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:
Timo Kinnunen <ti***********@bigfoot.com> scribbled the following:
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?


No, I'm answering the OP's question.
Jul 17 '05 #9

P: n/a
Timo Kinnunen <ti***********@bigfoot.com> scribbled the following:
On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:
Timo Kinnunen <ti***********@bigfoot.com> scribbled the following:
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?

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".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
Jul 17 '05 #10

P: n/a
Joona I Palaste wrote:
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?


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".


Isn't that exactly what set methods do?
Jul 17 '05 #11

P: n/a
Michael Borgwardt <br****@brazils-animeland.de> scribbled the following:
Joona I Palaste wrote:
>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?
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".

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 (pa*****@cc.helsinki.fi) ------------- 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
Jul 17 '05 #12

P: n/a
On Tue, 11 May 2004 12:51:49 +0200, Michael Borgwardt
<br****@brazils-animeland.de> wrote or quoted :
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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.