Java pass refers to a function

I got a little confused about the Java concept of "All omissions at a cost."

Consider the following code:

class Test { Integer A; String B; ... void SetVar(Object??? var, Object value) { // Set A variable to the value (considering that it possible) } } 

Can I encode the SetVar function so that the following code sets A to 2 and B to Hi ??

 void Init() { SetVar(this.A, 2); SetVar(this.B, "Hi"); } 
+4
source share
7 answers

In short, reassigning a value (which means the = operator) to an existing link does not change the object indicating the source link.

A minor misunderstanding in Java is that people think these are two types of variables:

  • Primitives (e.g. int, boolean, etc.)
  • Links (e.g. Integer, Boolean, custom objects, etc.)

Java NEVER uses links. The word Link is incorrect.

In Java, instead of Pointers, they are used only for managing objects.

To better understand the shadow: http://javadude.com/articles/passbyvalue.htm

As for your case, even if you skipped the Java naming conventions (but this is another topic), you can solve your problem by doing:

 void SetVar(Integer value) { this.A = value; } 

Indeed, if you pass A as a local parameter (like you), this local parameter will represent a copy of the A reference , since Java focuses only on passed-by-value , so changing it does not affect the start link.

+2
source

Yes, you can. Using reflection java api.

 class Test{ void SetVar(Field field, Object value) throws IllegalArgumentException,IllegalAccessException { field.set(this, value); } public static void main(String[] args){ Test test =new Test(); test.SetVar(Test.class.getDeclaredField("A"), 2); test.SetVar(Test.class.getDeclaredField("B"), "Hi"); } } 
+2
source

In general, it depends. In this particular case, A and B are immutable, you cannot.

If you have a mutable object with setters, you can change some of its properties in your setVar method:

 var.setValue(value); 

But you cannot change the link that the original object points to (because it was passed by value).

 var = someOtherObject; //no effect on the reference in the calling code 

More on this in this post , which is part of Java Chat .

+1
source

Yes, Java is passed by value, but you should also understand that when you pass an object to a specific method, you actually pass a link (pointer) to that object. In your case, the two arguments A and B are also immutable, which means that you can only reassign the local variable that represents them, instead of replacing the original objects that were passed to SetVar .

+1
source
 void SetVar(Object??? var, Object value) { 

to become

 void SetVar(final Integer var, final String value) { A=var; // use minuscule for field like a=var; B=value; // this. is not necessary here } 

Initialize constructor:

 Test (final Integer var, final String value){ setVar(var, value); } 
0
source

Integer and String are immutable, so after initialization you cannot change the value of A or B , just assign a new value to the variable. And since the var variable is local to the method inside SetVar , changing the object that var points to does not affect external callers.

0
source

You cannot do this because in your example you pass this.A to the setVar () method and this.A is null, so this is not useful. However, you can do it. Be the holder and pass the setVar () method to the holder.

The code below will do what you want. It uses IntegerHolder and StringHolder for your fields a and b and initializes the holder fields when building a class with empty holder objects.

The setVar () call then does not set the value of the a and b fields, instead sets the contents of the holders.

 public class Test { IntegerHolder a = new IntegerHolder(); StringHolder b = new StringHolder(); void setVar(IntegerHolder var, Integer value) { var.value = value; } void setVar(StringHolder var, String value) { var.value = value; } class IntegerHolder { Integer value; } class StringHolder { String value; } } 
0
source

Source: https://habr.com/ru/post/1439808/


All Articles