English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The uses of the helper class are as follows.
Provide common methods required by multiple classes in the project.
Helper methods are typically public and static, so they can be called independently.
Each method of the helper class should work independently of other methods in the same class.
The following example demonstrates such a helper class.
public class Tester { public static void main(String[] args) { int a = 37; int b = 39; System.out.println(a + " is prime: " + Helper.isPrime(a)); System.out.println(b + " is prime: " + Helper.isPrime(b)); } } class Helper { public static boolean isPrime(int n) { if (n == 2) { return true; } int squareRoot = (int)Math.sqrt(n); for (int i = 1; i <= squareRoot; i++) { if (n % i == 0 && i != 1) { return false; } return true; } } }
Output Result
37 is prime: true 39 is prime: false