Java static method

If you apply static keyword with any method, it is known as static method.

  1. A static method belongs to the class rather than the object of a class.
  2. A static method can be invoked without the need for creating an instance of a class.
  3. A static method can access static data member and can change the value of it.

Following is the example of static method


\begin{lstlisting}
//Java Program to demonstrate the use of a static method.
cl...
...y method
s1.display();
s2.display();
s3.display();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
\end{lstlisting}

Following is the another example of a static method that performs a normal calculation


\begin{lstlisting}
//Java Program to get the cube of a given number using the st...
...result=Calculate.cube(5);
System.out.println(result);
}
}
\end{lstlisting}

The output for the above program is 125.