4

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!

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
ShoulO
  • 448
  • 2
  • 7
  • 20

6 Answers6

9

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

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
4

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

public class IntData
{
   public int bee;
}

In MyCoding and Moo clases,

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;
    }
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

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

There is no notion of C style pointers in java, so you don't typically overwrite an integer value in java methods - rather, you set integers to the result of a calculation that occurs in a method . Or, more commonly, you define objects, and use getters and setters in those objects to get/set values.

In your case... you must either (1) declare the integer you are calculating in the method as a global variable or (2) return the calculated value to the calling class, and do the setting in the class that originally declared/accesses the variable of interest.

1) You can either make the "mee" variable a static, global variable, and edit it anywhere

or

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

jayunit100
  • 17,388
  • 22
  • 92
  • 167
1

1st of all i dont find any variable bee, 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;
    }
}
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • yes this is good if i have only one variable, what if i have several of them - like Moo.moo(mee,zee,vee) – ShoulO Dec 15 '11 at 03:59
  • thanks for reply. I don't understand what "(this)" means? (also my typo mistake - should be Moo.moo(this) instead of Gryb.moo(this), sorry). Usage of "this" was allways a confusion to me – ShoulO Dec 15 '11 at 04:18
  • this is the currently running instance of the class. you must read in details about it. It is very much useful – dku.rajkumar Dec 15 '11 at 04:34
1

IN your code

static int mee=1;

and trying to change

mee=76547545;

both are different. When your declare a variable as static that variable must called by ClassName in other classes. You followed correct here.

Moo.moo(mee);

Change the mee=76547545; line as

MyCoding.mee=76547545;

And run the code.Enough;

alishaik786
  • 3,696
  • 2
  • 17
  • 25
0

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

try this

mee =  Moo.moo(bee);
user710502
  • 11,181
  • 29
  • 106
  • 161