Section 6.7 Static Methods
We have already discussed the most common static method of all,
main. However in our Fraction class we also implemented a method to calculate the greatest common divisor for two fractions (gdc). There is no reason for this method to be a member method since it takes two Integer values as its parameters. Therefore we declare the method to be a static method of the class. Furthermore, since we are only going to use this gcd method for our own purposes we can make it private.
private static Integer gcd(Integer m, Integer n) {
while (m % n != 0) {
Integer oldm = m;
Integer oldn = n;
m = oldn;
n = oldm%oldn;
}
return n;
}
You have attempted of activities on this page.
