944,103 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9892
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 21st, 2006
0

Return values from a method by not using return

Expand Post »
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 :
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Similar Threads
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
May 21st, 2006
0

Re: Return values from a method by not using return

Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
May 21st, 2006
0

Re: Return values from a method by not using return

Quote 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.
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
May 22nd, 2006
0

Re: Return values from a method by not using return

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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
May 22nd, 2006
0

Re: Return values from a method by not using return

Quote 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:

Quote ...
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
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
May 22nd, 2006
0

Re: Return values from a method by not using return

Quote 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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
May 22nd, 2006
0

Re: Return values from a method by not using return

So effectively you have in Java ALWAYS the equivalent of passing a "
"const &" in C++.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
May 23rd, 2006
0

Re: Return values from a method by not using return

Quote ...
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?

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
May 23rd, 2006
0

Re: Return values from a method by not using return

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:

Java Syntax (Toggle Plain Text)
  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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
May 25th, 2006
0

Re: Return values from a method by not using return

ah ok., thats makes sense now.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Adding an icon to JFrame
Next Thread in Java Forum Timeline: installing sqlexplorer in eclipse





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC