troubleshooting Question

Any clarifying explanations please ?

Avatar of krakatoa
krakatoaFlag for United Kingdom of Great Britain and Northern Ireland asked on
Java
28 Comments1 Solution120 ViewsLast Modified:
I've got a reasonable knowledge of Java, but this quote from Java in a Nutshell 6th Ed., seems a bit unsettling for some reason to me :

The following code shows that even after a call to manipulate(), the value contained in variable c is unaltered - it is still holding a reference to
a Circle object of radius 2. If Java was [sic] a pass-by-reference language, it would instead be holding a reference to a radius 3 Circle:

   
public void manipulate(Circle circle){
    
        circle = new Circle(3);
        
    }
    
    Circle c = new Circle(2);
    manipulate(c);
    System.out.println("Radius: "+ c.getRadius());

 . . . when contextualised thusly :

class RefDemo {

Circle c;

    public int /*void*/ manipulate(Circle circle){
    
        circle = new Circle(3);
        return circle.getRadius();
        
    }

    
    public RefDemo(){this.c = new Circle(2);}
    
    public static void main(String[] args){
  
        RefDemo rd = new RefDemo();
        
        System.out.println("Radius:"+(rd.manipulate((rd.c))));
        System.out.println("Radius: "+ rd.c.getRadius());

    }
  
  
  public class Circle {
  
  private int radius;
  
    public Circle(int r){
    
        this.radius = r;
    
    }
    
    public int getRadius(){return this.radius;}
  
  }
  
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 28 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 28 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros