How to use Pass by Value in Java
public class Test {
public void Receiving (int var)
{
var = var + 2;
}
public static void main(String [] args)
{
int passing = 3;
Receiving (passing);
System.out.println("The value of passing is: " +passing);
}
}
When i compiled this code... i got this error.
Test.java:12: non-static method Receiving(int) cannot be referenced from a static context开发者_StackOverflow中文版
Receiving (passing);
^
Now When i changed my method Receiving into static
, the output was....
The value of passing is: 3
Now how can i pass the value 3 into the method Receiving so that the output is 5 printed in the console.
This is generally not possible in Java, because primitive types are always passed by value, not by reference. Even if you used the Integer
wrapper class instead of int
, you couldn't change that because Integer
is immutable.
You can, however use this trick:
public class Test {
// static is necessary, as you already found out
public static void Receiving (int[] var)
{
var[0] = var[0] + 2;
}
public static void main(String [] args)
{
int[] passing = new int[] {3};
Receiving (passing);
System.out.println("The value of passing is: " +passing[0]);
}
}
That wouldn't be pass-by-value, that would be pass-by-reference! Pass-by-value effectively means that the method works on a copy of the original variable. (See http://en.wikipedia.org/wiki/Evaluation_strategy). Java is always pass-by-value.
For primitive types (e.g. int
, float
, etc.), if you want to have the input value modified by the method, you can simply return it:
public static int Receiving(int var) {
return var + 2;
}
...
passing = Receiving(passing);
Java is pass by value, but you have to be clear about what's passed: it's the reference to an object or value, not the object or value itself. You can't modify that reference, but you may be able to change the state of the object it points to (as long as it's mutable).
so you'll never see the output "5" in your console. That's not how Java works. The JVM has already made the clear.
Now how can i pass the value 3 into the method Receiving so that the output is 5 printed in the console.
You could wrap the integer so that it will be passed as a refernce:
public class Test {
class Wrapper {
int var;
Wrapper(int val) {
var = val;
}
}
public void Receiving(Wrapper w) {
w.var = w.var + 2;
}
public static void main(String[] args) {
Test test = new Test();
Wrapper passing = test.new Wrapper(3);
test.Receiving(passing);
System.out.println("The value of passing is: " + passing.var);
}
}
Cannot do it with "int" -- there is only "pass-by-value" in Java. Still, can simulate easily w/ AtomicInteger:
static void test(AtomicInteger i)
{
i.addAndGet(2);
}
public static void main(String[] args) throws IOException
{
AtomicInteger i = new AtomicInteger(3);
test(i);
System.out.println(i);
}
A non-static member function can only be invoked on an object instance. It can't be called "on its own".
You have two options:
- make
Receiving
static; - create an instance of
Test
in main and calltest.Receiving(3)
.
Either way, var
will be passed into the function by value, so main
will not see the changed value. To get around that, you have several options, which include:
- making
var
into a single-element array, - using
AtomicInteger
(it isn't really designed for this, but I often see it used in this manner).
It often much clearer in Java for the caller to use a function as you should expect methods to change the object but not its arguments.
public static int receiving (int var) {
return var + 2;
}
public static void main(String... args){
int passing = 3;
// its clear passing can be changed.
passing = receiving(passing);
System.out.println("The value of passing is: " +passing);
}
The primitive type are passed by value. Java Objects are always referenced and its reference is always passed by value.
As for the static error, there's two ways to resolve it:
- Make
receiving
static
, or: - create a
Test
object and callreceiving
.
Example:
Test test = new Test();
test.Receiving(passing);
To pass by reference, you will have to pass a reference of the object to the method (of which java will pass the reference by value) and do the calculation from there. An array of int or AtomicInteger
will do.
What others have said above is quite true. But if you just want to get 5 as your final output there are numerous ways to achieve that. I have posted below one of them.
public class NewClass {
static int passing = 3;
public static void Receiving (int var)
{
var = var + 2;
passing = var;
}
public static void main(String [] args)
{
Receiving (passing);
System.out.println("The value of passing is: " +passing);
}
}
I know this is not a good approach but i felt you were hell bent on getting 5 printed....
Passing by reference isn't possible in Java, and Objects aren't passed at all: Article on passing
You could however
- Use a return value in your function and use that value in your main program
- Declare a static value at the top. Then your value would have a scope of the whole program though, not sure you want that
精彩评论