Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Need help passing variable to another class

    I'm trying to call the variable r from another class, but I'm having trouble making it.

    Code:
    public class Circle {
        
        final double PI = 3.14159;
        CircleTester temp = new CircleTester();
        double radius = temp.gettemprad();  //this is where i was gonna call a method but I was having trouble making one in my other class
        
        Circle(double newradius){
            newradius = radius;
        }
        Circle(){
            radius = 0.0;
        }
        void setRadius(double inputradius){
            radius = inputradius;
        }
        double getRadius(){
            return radius;
        }
        
        double getArea(){
           return PI * radius * radius;
        }
        
       double getDiameter(){
           return radius * 2;
        }
       double getCircumference(){
           return 2 * PI * radius;
       }
    }

    Code:
    import java.util.Scanner;
    public class CircleTester {
        public static void main(String [] args){
           double r;
           Circle c = new Circle();
            Scanner scan = new Scanner(System.in);
            
            System.out.println("What is the radius of the circle");
            
            r = scan.nextDouble();
            
            //after I got r to the other class i was going to call the methods below
                    
            System.out.println("The circle's area is "+c.getArea());
            
            System.out.println("The circle's diameter is "+c.getDiameter());
            
            System.out.println("The circumference is "+c.getCircumference());
        
        }
        
    }

    Basically I want the user input (r) to go to the other class so I can get different measurements from it and then return those values back to the original class using System.out.print. Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Since the datatype is primitive, you cannot pass it around and change it. Only objects work via object pointer references in java (and only with mutable object types).
    You can pass it to the circle's setRadius method to set the value in the class instance of Circle. Changing it within circle will not modify it in the main, for that you would need to retrieve the value again from the getRadius method (or whatever you are fetching).

  • Users who have thanked Fou-Lu for this post:

    CodyJava (12-03-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •