Search code examples
javaclassgreatest-common-divisor

How to use a value returned in a different class?


This is probably a very simple question. Say I had a class that computed the gcd, called Gcdcomp. The code in that class all works. When i refer to it in my main block of code, I say..

Gcdcomp.getGcd(a, hii);

a and hii are my two variables. By default, the Getgcd class uses int a and int b and will return a after executing euclids algorithm. How do I use that returned value as a variable in my main code?


Solution

  • You can assign the result of a function call directly to a variable, like this (assuming that getGcd returns an int):

    int result = Gcdcomp.getGcd(a, hii);
    

    Or if result is already declared beforehand, you can omit the declaration, like this:

    result = Gcdcomp.getGcd(a, hii);