Return values from a method by not using return

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 42
Reputation: j1979c is an unknown quantity at this point 
Solved Threads: 0
j1979c's Avatar
j1979c j1979c is offline Offline
Light Poster

Return values from a method by not using return

 
0
  #1
May 21st, 2006
Just want to confirm if it is possible to set in this case (Example 1) class Two's pumpId variable by sending it as a parameter to class One's setPumpId method without using the usual return method assignment like :
  1. pumpId = o.getPumpNum()
I think in C++ we can do that, but just ain't too sure with JAVA and I'm having problem returning more than one value from a method with just a return statement in the end which I think is impossible. :-|


Example 1:
  1. public Class One {
  2.  
  3. private int pumpNum = 3;
  4.  
  5. public void setPumpId(int pumpId)
  6. {
  7. pumpId = pumpNum;
  8. }
  9.  
  10. }
  11.  
  12. public Class Two {
  13.  
  14. private int pumpId = 0;
  15. private One o = new One();
  16.  
  17. o.setPumpId(pumpId);
  18.  
  19. }

Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 786
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Master Poster

Re: Return values from a method by not using return

 
0
  #2
May 21st, 2006
Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 42
Reputation: j1979c is an unknown quantity at this point 
Solved Threads: 0
j1979c's Avatar
j1979c j1979c is offline Offline
Light Poster

Re: Return values from a method by not using return

 
0
  #3
May 21st, 2006
Originally Posted by Phaelax
Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.
Well, I am looking for a way to pass a variable from one class as a parameter, to another class's method and set it there, without using a return statement from the other class's method.

Or is there any way to return more than one value from a method.

Hmm....maybe the scenario i put up earlier is unclear and ambiguous. Sorry

It has to be between two classes though. MultiThreading's kinda tough with JAVA :mad: , put 4 petrol pumps and 20 customers and the system's crawling like a snail... :cry: :-| :eek: ...wish I could do it in Delphi...

Thanks for the reply anyway.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 786
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Master Poster

Re: Return values from a method by not using return

 
0
  #4
May 22nd, 2006
Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 42
Reputation: j1979c is an unknown quantity at this point 
Solved Threads: 0
j1979c's Avatar
j1979c j1979c is offline Offline
Light Poster

Re: Return values from a method by not using return

 
0
  #5
May 22nd, 2006
Originally Posted by Phaelax
Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.
Yeah just found out this from the web:

O'Reilly's Java in a Nutshell by David Flanagan puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'"
Forgot that JAVA behaves like that cause there are no * and & to use.....still in the C++ mindset....hehe :cheesy:

Guess I have to pass the object to change the value of the class's variable instead of passing just the variable.

Thanks a lot! This is refreshing....
:o
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Return values from a method by not using return

 
0
  #6
May 22nd, 2006
Originally Posted by Phaelax
Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.
WRONG WRONG WRONG!

Objects are NEVER passed in Java AT ALL.
Neither is anything ever passed by reference.

Instead references are passed by value.
You can NOT change the reference and expect the one in the calling method to change.
You can however change datamembers on the reference and those changes will be reflected because they're actual changes on the reference that's residing inside the calling method.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Return values from a method by not using return

 
0
  #7
May 22nd, 2006
So effectively you have in Java ALWAYS the equivalent of passing a "
"const &" in C++.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 786
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Master Poster

Re: Return values from a method by not using return

 
0
  #8
May 23rd, 2006
Instead references are passed by value.
how do you pass a reference by value? that doesn't even make sense. unless you saying the value of the memory address to the reference?
Maybe I'm misunderstanding what you're trying to say, are you saying this wouldn't show affect on the object outside the method?

  1. public static void main(String[] args)
  2. {
  3. Thing thing = new Thing(15);
  4. System.out.println(thing.getX());
  5. resetX(thing,42);
  6. System.out.println(thing.getX());
  7. }
  8.  
  9. public static void resetX(Thing t, int x)
  10. {
  11. t.setX(x);
  12. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Return values from a method by not using return

 
0
  #9
May 23rd, 2006
no, you misunderstand.

the reference is passed by value. That means you cannot change the reference itself, but you can change the content of the thing you're referencing.
So in your case the object referenced by t gets its data changed.
But the following would not work:

  1. public static void resetX(Thing t, int x)
  2. {
  3. t = new Thing(x);
  4. }

The Thing outside the method would not get replaced by the new one, which goes out of scope on leaving the method and is lost forever.
Were Java using pass by reference (which it doesn't), the object outside the method would get replaced by the new one because the reference to it would be replaced by a new one.
Since Java uses pass by value, you're actually passing a copy of the reference to the method, so another reference pointing to the same memory space (thus Object).

It's a trap many beginners fall into, especially those coming from a C or C++ background where pass by reference (or passing pointers) is the standard way and such things are possible).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 786
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Master Poster

Re: Return values from a method by not using return

 
0
  #10
May 25th, 2006
ah ok., thats makes sense now.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC