What are helper objects in java?

后端 未结 6 406
无人共我
无人共我 2021-01-30 11:04

I come across few of the times called helper objects... can anybody elaborate what are those helper objects and why do we need them?

6条回答
  •  生来不讨喜
    2021-01-30 11:12

    You can see a helper class as a toolbox that can be used by other classes to perform task like testing if a string is a palindrome, if a given number is prime, if an array contains negative number etc. You can create helper class by making all its methods static and its constructor private, and optionally you can also make the class final. Thus it can not be instantiated and one can easily access to all its methods directly.

    public final class HelperClass{
    
           private HelperClass(){
           }
    
           public static boolean isPositive(int number) {
    
                 if (number >= 0) 
                        return true;
                 else
                        return false;
           }
    }
    

    Here the function can be use directly to test a number :

     HelperClass.isPositive(5);
    

提交回复
热议问题