when i pass variables to my method they are not updated in the main code, but only passed to the method. how to do that once passed variable would update in 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!
Passing Primitive Data Type Arguments
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.
Passing Reference Data Type Arguments
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
To get the behavior you are expecting, you will need to return a 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 a Constructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With