Friday, November 19, 2010

Java's Static Methods

A Java method can be of type static and unlike an instance method where the object has to be used to call it, a static method must be prefixed with a class name.

An example below from further illustrates the illustration and call of static methods.
[+/-] show/hide

Java static method example
This Java Example shows how to declare and use static methods inside
a java class.

public class StaticMethodExample {

public static void main(String[] args) {

int result = MathUtility.add(1, 2);
System.out.println("(1+2) is : " + result);


class MathUtility{

* To declare static method use static keyword.
* Static methods are class level methods and can not access any instance
* member directly. However, it can access members of a particular object
* using its reference.
* Static methods are generally written as a utility method or it performs
* task for all objects of the class.

public static int add(int first, int second)
return first + second;


No comments: