| | |
Return values from a method by not using return
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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 : 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:
Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:
Java Syntax (Toggle Plain Text)
pumpId = o.getPumpNum()
Example 1:
Java Syntax (Toggle Plain Text)
public Class One { private int pumpNum = 3; public void setPumpId(int pumpId) { pumpId = pumpNum; } } public Class Two { private int pumpId = 0; private One o = new One(); o.setPumpId(pumpId); }
Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:
•
•
•
•
Originally Posted by Phaelax
Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.
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.
•
•
•
•
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.
•
•
•
•
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.'"
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 •
•
•
•
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.
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.
•
•
Join Date: Mar 2004
Posts: 786
Reputation:
Solved Threads: 39
•
•
•
•
Instead references are passed by value.
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)
public static void main(String[] args) { Thing thing = new Thing(15); System.out.println(thing.getX()); resetX(thing,42); System.out.println(thing.getX()); } public static void resetX(Thing t, int x) { t.setX(x); }
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:
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).
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)
public static void resetX(Thing t, int x) { t = new Thing(x); }
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.
![]() |
Similar Threads
- how to send data in POST method using javascript (JavaScript / DHTML / AJAX)
- Constructor Question? (C#)
- Help: Passing arrays between functions (C)
- Help on Assignment (Java)
- Display Value in JcomboBox (Java)
- Accessing a variable of another class (Java)
- importing graphs onto GUI frame (Java)
- how will i repeat my whole game program? (Java)
Other Threads in the Java Forum
- Previous Thread: Adding an icon to JFrame
- Next Thread: installing sqlexplorer in eclipse
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






