473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing by reference, more confusion...

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
12 2839
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
oh, and in Java there is no such thing as passing by reference
Jul 17 '05 #3
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
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
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
1
by: Oliver | last post by:
I'm an expert C# programmer (honest) but... the following code prints 'Oranges', not 'ORANGES' - this confuses me because I thought reference types (eg string) where passed by...err..reference -...
2
by: Mat Andrews | last post by:
Hi, I'm comfused with this peice of code which I'm looking at. I can't figure out how it works (and it does appear too); ApplicationData appData = new ApplicationData(); ...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.