How to accept a public static variable as parameter in Java [duplicate] - java

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I have the following instantiated variable
public static int r1 = 10;
I have several of these variables r1 - r4, I want to write a method that will be able to take the variable r1 as a parameter of the method and increment it.
Pseudo code would look like :
r1 = r1 + 1;
My question is how can I take the variable as a parameter of one method instead of writing 4 different methods to accomplish this?

You can't, because Java does not let you pass variables of primitive types by reference.
You can put your variables into an array, and pass an array and an index:
public static int[] r = new int[4];
...
public static void modify(int[] array, int pos) {
array[pos]++;
}
...
modify(MyClass.r, 1); // instead of modify(MyClass.r1);
Alternative approach is to return the modified value to callers, and let them assign it back:
public static int modify(int orig) {
return orig+1;
}
...
MyClass.r1 = modify(MyClass.r1);

Related

The array value changed after calling static method with the array Java [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 years ago.
The value of array elements are getting changed while calling a static method with the parameter passing the same array as argument but primitive value does not get changed
Is it work differently in the case of while we pass data structure as method argument ?
Want to know why array element get changed after the method call with the array ..
I was expecting value 0 in the last syso statement instead of 999
public class TestStatic {
public static int square (int x)
{
x = x + 1;
return x * x;
}
public static int[] changeArr(int a[])
{
a[0] = 999;
return a;
}
public static void main(String[] args)
{
int a = 10;
System.out.println((square(a)));
System.out.println(a);
int arr[] = {0,0};
changeArr(arr);
System.out.println(arr[0]);
}
}
Actual Output:
121
10
999
I was expecting
121
10
0
In Java, method parameters are by reference, not by value, which means you pass the reference to the object, not a copy of the object. Note that primitives are passed by value.

Java variable scope in and outside loop [duplicate]

This question already has answers here:
Java variable may not have been initialized
(7 answers)
Why does the Java compiler not understand this variable is always initialized?
(3 answers)
Closed 2 years ago.
I wrote this code in an online java test recently. It was inside a method that was set to return an integer. I got an error message something like "variable a has no value assigned to it". I find this odd because the forloop must have access to the methods variable and the assignments inside of the loop must be registered right?
int a;
for(int i=1;i<5;i++){
a = i;}
return a;
I did assume that the method would return the integer 5.
This is only a question regarding scope of the variable a. I know that the code makes no sense.
You can try below code , that will help you. Initialisation is mandatory for any variable to return or hold any value.
class a{
public static void main(String[] args) {
System.out.println(test());
}
public static int test(){
int a = 0;
for(int i=1;i<5;i++){
a = i;}
return a;
}
}

Static recursive method in Java doesn't update variable value [duplicate]

This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.

Return value of a void function [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.. If it doesn't return a value than it is void..
However in the below program, I'm able to change the values of an array from a void function. Can anybody explain this to me please?
public class ArraysInMethods {
public static void main(String args[]){
int rd[] = {2,3,4,5,6};
change(rd);
for(int y: rd){
System.out.println(y);
}
}
public static void change(int x[]){
for(int counter = 0; counter<x.length;counter++){
x[counter]+=5;
}
}
}
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.
That is correct only for methods returning values, i.e. methods other than void. These methods define expressions, while void methods define statements.
Calling your change method is a statement, in the sense that it lacks return values. It does not mean, however, that it cannot change the state of your running program - for example, give different values to variables.
However in the below program, I'm able to change the values of an array from a void function.
You are not returning a value from your void function; all you do is modifying the array in place. This is allowed, because arrays are passed by reference.

I don't understand how Java is pass by value [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
So I thought Java was pass by value but why does the following code:
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
phase1(numbers);
for (Integer number: numbers) {
System.out.println("number = " + number);
}
}
static void phase1(List<Integer> numbers) {
numbers.remove(0);
}
Print the following?
number = 2
number = 3
So I thought Java was pass by value
It is.
...but why does the following code...print the following?
Because the value passed for reference types is the reference, not a copy of the object.
"Pass by reference" refers to a completely different thing, passing a reference to the variable. Java doesn't have that.
If Java had pass-by-reference, then this change to your method:
static void phase1(List<Integer> &numbers) {
// Pass-by-reference indicator --^--- that Java doesn't actually have
numbers = new ArrayList<Integer>()
}
...would make your code print no numbers at all. But it doesn't, so it doesn't.
Think of the object reference as an int or long that uniquely identifies the object elsewhere in memory. (Which is basically what it is.) Just as a = b with ints copies b's value into a, so a = b copies b's value into a even when the value is an object reference. The value being copied is the reference, not the object it refers to.

Categories