0
public class Shape 
{
    public static void main(String args[]){
        int num = 0;
        cng(num);
    }

    public static void cng(int x){
        x = 52;
        System.out.println(x);
    }

}

As you can see, in the cng Method I set the value of x to 52 and then print out the value of x.

Then, back in the main method, the cng method is performed on the num variable.

What I want to do, however is set the value of 52 to x without the System.out.println(x); function in my cng method and print out the value in my main method. How would I go about doing that?

I tried doing

public static void cng(int x){
    x = 52;
}

and then

public static void main(String args[]){
    int num = 0;
    cng(num);
    System.out.println(num);
}

but it only prints out a 0 because num is set to 0. I thought that performing cng on the num variable would change it to 52, but it doesn't.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
Space Ghost
  • 765
  • 2
  • 13
  • 26
  • It changes a copy. Make num a field. – keyser Nov 23 '12 at 08:26
  • change return type of `cng()` to int, and return int from `cng()` to main method – Nandkumar Tekale Nov 23 '12 at 08:27
  • [Immutable objects](http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html) can not be changed like this, Please also read [is java pass by reference](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Bo PENG Nov 23 '12 at 08:34

5 Answers5

2

make your cng method return an int variable

public static int cng(int num){    
num = 52; 
return num; 
}

In Your Main method, assign the returned variable from cng() method

int num = 0;
 num =cng(num);
 System.out.println(num);

Or:

you could always, make num as a member static variable,

   static int num;
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

You want to pass the data by reference. This is not possible for primitive values in Java (int, double, boolean, etc).

You have the following options:

  • Make it a member of the class
  • Create a wrapper object (object references are also passed by value, but you can change the members of that object in a function, just not the object reference itself)
  • Return the value in the function (as mentioned by other answers)
Geerten
  • 1,027
  • 1
  • 9
  • 22
  • partly wrong, Objects are not passed by reference in java . java is always, pass by value in case of both objects and primitives. here's a very good explanation i would suggest. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – PermGenError Nov 23 '12 at 08:37
  • @GanGnaMStYleOverFlowErroR : oppa gangnam style . You might wany tp refer : http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Mukul Goel Nov 23 '12 at 08:41
  • 1
    Actually correct most would be to say : java passes object refferences by value. – Mukul Goel Nov 23 '12 at 08:43
  • @GanGnaMStYleOverFlowErroR : yea , i think it is neither objects by value nor object as reference it is object references by value. Just for a ref: i posted before you edited ypur initial commemt :-) – Mukul Goel Nov 23 '12 at 08:47
  • I knew it is like Mukul Goel stated, but I thought that was a bit too complicated for the questioner. In practice it boils down to objects being passed by reference. – Geerten Nov 23 '12 at 09:25
1

Try to change your code to this:

public class Shape 
{


public static void main(String args[]){
    int num = 0;
    num = cng(num);
    System.out.println(num);
}

public static int cng(int x){
    x = 52;
    return x;
}

}
djakapm
  • 175
  • 7
1

read along the comments :

public class Shape 
{

    public static void main(String args[]){
    int num = 0;
    num = cng();   //store value returned by cng() in num
    System.out.println("num : " +num); // display num
    }

    public static int cng(){    //change return type to int
    return 52;
    }

}
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
0

The reason for this behaviour is that arguments are passed by value in Java. This implies that it is simply a copy of the variable that you passed to it. Thus the assignment that you do is only local to the method.

Karaaie
  • 527
  • 5
  • 15