Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Static Method - Method Calling Without an Object in Java

  1. public class Static{
  2. public static void main(String[] args) {
  3. int result = MathUtility.add(1, 2);
  4. System.out.println("(1+2) is : " + result);
  5. }
  6. }
  7. class MathUtility{
  8. /*
  9. * To declare static method use static keyword.
  10. * Static methods are class level methods and can not access any instance
  11. * member directly. However, it can access members of a particular object
  12. * using its reference.
  13. *
  14. * Static methods are generally written as a utility method or it performs
  15. * task for all objects of the class.
  16. *
  17. */
  18. public static int add(int first, int second)
  19. {
  20. return first + second;
  21. }
  22. }