Passing a Reference Variable

Wed Feb 04, 2009 11:31 pm

Passing a Reference Variable :
When you pass a reference variables to a method ,you pass a copy of the passed reference variables .Both references points to the same object

Note : Both references can change a member in the object .

Example :
Code:
public class Main {

     
           
    public static void main(String[] args) {
       
          Main test=new Main();
          circle newc=new circle();
          newc.setR(43);
          System.out.println("Before calling method : R="+newc.getR());
          test.changeR(newc);
          System.out.println("After calling method : R="+newc.getR());
         
    }
    public void changeR(circle c)
           
    {
        c.setR(32);
        c=null;
    }
}

interface shape
{
     
     public void Draw();
     
     
}
class circle implements shape
{
    private int R;
    public void Draw() {
        System.out.println("Drawing Circle here");
    }

    public void setR(int R) {
        this.R = R;
    }

    public int getR() {
        return R;
    }
   
     
     
}


The result :
Code:
Before calling method : R=43
After calling method : R=32




  Related Posts  to : Passing a Reference Variable
 different between Local variable and Global variable     -  
 object reference be cast to an interface reference     -  
 Array Passing     -  
 Passing an Argument to a Function by Value     -  
 JSP Passing Arrays to Methods     -  
 Passing Pointers to function example     -  
 passing string value from java to .exe file     -  
 Passing Enum as Type Parameter to method     -  
 Passing arrays as function parameter in java     -  
 transient variable     -  

Topic Tags

Java OOP