How would you pass a java integer by reference to another function

Showing Answers 1 - 13 of 13 Answers


  • Jun 17th, 2005

Passing by reference is impossible in JAVA but Java support the object reference so. 
Object is the only way to pass the integer by refrence. 


  • Jul 26th, 2005

Create a wrapper class Integer of int. Now u can pass the Integer class by reference.

G.Laxmi Narsaiah

  • Aug 14th, 2005

what is wrapper class ? 
give me example for this pass a java integer by reference to another function  
plz send to this mail id

Pramod Sisodiya

  • Aug 16th, 2005

U can pass int, as a refrence through integer wrapper class. But still ur purpose not solved through this to change the value of actual integer variable. If u want to get changes through function then u have to use Int handler.

  Was this answer useful?  Yes

Siva Gandi

  • Oct 4th, 2005

you can use int array of length 1 instead

  Was this answer useful?  Yes

Ravi Kumar

  • Oct 17th, 2005

In Java,

Primitive Types are passed by value

Objects are passed by reference

Note: No reference will be passed incase of Remote access.

Same object can be referenced with in the JVM only.


  • Apr 6th, 2007

wrapper class is a class that allow primitive data types to accessed as objects


  • Jul 30th, 2008

Java is neither Pass By value or Pass By reference.

Let say the functiion is
public function(int number)

//code which calls the above function
int num = 10;

so num here points to 10 , the num has some adress . The address of num is copied to the number and both this num and number will point to 10.

  Was this answer useful?  Yes

Java is only pass-by-value language. Passing a reference is not possible though you can pass by value. If you want changes to reflect back then consider the primitive type in a custom class not a wrapper class i.e., to say, this class will have getter and setter methods. Pass this object by value to another function. The value passed is the memory location to the function. This seems like pass-by-reference. Modify the value from the function - it will be reflected in the original. JOB DONE.


class ContainInt {
    int intVal;        //primitive type
    public ContainInt(int i) {
        intVal = i;
    public void setIntVal(int i) {
        intVal = i;
    public int getIntVal() {
        return intVal;
    public String toString() {
        return String.valueOf(intVal);

public class Temp {
    private void modVal(ContainInt intVal) {
        //intVal = new ContainInt(20);        //don't do this, will break ref.
    public static void main(String[] args) {
        ContainInt intVal = new ContainInt(10);
        System.out.println("Before modification : " + intVal);
        new Temp().modVal(intVal);
        System.out.println("After modification : " + intVal);

  Was this answer useful?  Yes

what is point behind sending integer by reference???
Still if you want to do so... look
class Demo{
private int i;
public static void main(String[] ar){
      Demo d=new Demo();
      System.out.println(d.i); //will print 10
      System.out.println(d.i);   // will print 20

public demoFunction(Demo d){

i hope this will satisfy you.......

  Was this answer useful?  Yes


  • Mar 13th, 2018

I don't think that is right because if it was then changing the value inside the called function should reflect the change when the passed variable is accessed in calling function.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions