Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Passing a Reference Variable

Thu Feb 05, 2009 12:31 am

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




Post a reply
  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