How to update variables passed to a method?

when I pass variables to my method, they are not updated in the main code, but only passed to the method. how to do this if the passed variable is updated in the main code? Thanks!

//////////// here is main code: public static class MyCoding extends MainScreen{ static int mee=1; public static void myCode(){ Status.show("mee="+mee, 2000); // shows me=1 Moo.moo(mee); Status.show("mee="+mee, 2000);// should be mee=76547545 but still shows mee=1 ! } } //////////// here is my method: public static class Moo extends MainScreen{ public static void moo(int bee){ mee=76547545; return; } } 

What to do? Thanks!

+4
source share
6 answers

Passing primitive data type arguments

Primitive arguments, such as int or double, are passed to methods at a cost. This means that any changes in the values ​​of the parameters exist only within the framework of the method. When the method returns, the parameters disappear and any changes to them are lost.

Passing Arguments of Reference Data Type

Reference data type parameters, such as objects, are also passed to methods at cost. This means that when the method returns, the pass-in reference still refers to the same object as before. However, object field values ​​can be changed into a method if they have the proper access level.

To get the behavior you expect, you will need to return the value and assign it to the original variable.

 mee = Moo.moo(mee); 

And in your method:

 public static int moo(int mee) { // some processing mee += 76547545; return mee; } 

Passing Information to a Method or Constructor

+8
source

The parameters of the Java method are pass-by-value , so you cannot get the updated value. You need to create a class with an int field and pass a reference to the object as an argument.

 public class IntData { public int bee; } 

In the sections MyCoding and Moo

 public static class MyCoding extends MainScreen{ static IntData mee=new IntData(); public static void myCode(){ mee.bee=1; Status.show("mee="+mee.bee, 2000); Moo.moo(mee); //object reference will be copied Status.show("mee="+mee.bee, 2000); } } public static class Moo extends MainScreen{ public static void moo(IntData ref){ ref.bee=76547545; } } 
+4
source

Java passes primitives (ints / floats / strings) to methods by COPYING their VALUE. On the other hand, other OBJECTS are not copied when passed to the method (that is, other objects retain their state and can be changed by an external method).

There is no concept of C style pointers in Java, so you usually don't overwrite an integer value in java methods - rather, you specify integers as a result of the calculation that happens in the method. Or, as a rule, you define objects and use getters and setters in these objects to get / set values.

In your case ... you must either (1) declare the integer that you compute in the method as a global variable, or (2) return the computed value to the calling class and configure in the class that initially declares / accesses the variable of interest.

1) You can either turn the variable "mee" into a static, global variable, and change it anywhere

or

2) You can edit the "moo" method to RETURN an integer, and set the mee method to this return value.

+3
source

First of all, I do not find any bee variable, I think you mean mee . just pass the current object as this

 //////////// here is main code: public static class MyCoding extends MainScreen{ private int mee=1; private int beee=1; private int cee=1; public void myCode(){ Status.show("mee="+this.mee, 2000); // shows me=1 Moo.moo(this); // small change here Status.show("mee="+this.mee, 2000);// should be mee=76547545 but still shows mee=1 ! } } //////////// here is my method: public static class Moo extends MainScreen{ public static void moo(MyCoding obj){ obj.bee=76547545; obj.mee = 67798879; obj.cee =89789; } } 
+1
source

In your code

 static int mee=1; 

and try to change

 mee=76547545; 

both are different. When you declare a variable as static , that variable should be called by ClassName in other classes. You followed correctly here.

 Moo.moo(mee); 

Change the line mee = 76547545; as

 MyCoding.mee=76547545; 

And run code.Enough;

+1
source

The answer is simple, you are not assigning a value back to mee

try it

 mee = Moo.moo(bee); 
0
source

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


All Articles