

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create your own helper class in Java?
A helper class serve the following purposes.
Provides common methods which are required by multiple classes in the project.
Helper methods are generally public and static so that these can be invoked independently.
Each methods of a helper class should work independent of other methods of same class.
Following example showcases one such helper class.
Example
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
37 is prime: true 39 is prime: false
- Related Questions & Answers
- Build Your Own Botnet
- How to write your own header file in C?
- How to build your own Sqlite database in Python
- How to write your own LaTeX preamble in Matplotlib?
- Implement your own itoa() in C
- Write your own memcpy() in C
- Write your own atoi() in C++
- How will implement Your Own sizeof in C
- How to create our own/custom functional interface in Java?
- How to Build your own website using Django in Python
- Print with your own font using C#
- Implement your own sizeof operator using C++
- Print with your own font using Python?
- Write your own memcpy() and memmove() in C++
- Making your own custom filter tags in Django
Advertisements