What is a Static Method in Java?

Making your method static means that you do not need to instantiate an object in order to access that method of a certain class. It can be directly accessible to the main program by simply calling the class name and the class method.

How to have static method in Java?

To make a static method, just add a ‘static’ word before the data type of the method name. For example:

public static int methodname();

How to call the Static Method in the main Program?

You can call the static method in the main program like this:

className.methodName();

It also works if you declare an object for the class to access the method but that is discourage, because in the first place static is considered as a short cut on accessing the method of the class even without the object declaration (though in my other examples here with a static method, I still declared an object for the methods, it’s your turn to make the practice correct, I shall mind this practice in my future codes).

See Also:

How to Run Java Code Online

Like always, you can see how to use it, and how it behaves by providing a complete running program for it. For the sample java program on Static method, see the source code and the program algorithm below.

The program is just simple, we will just show how this static method works. So, we will just output a string inside a method. If you have a program in mind, try using static method and access it directly in your main program.

Java Book

Other Sample Java Codes

Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Program: How to Use Switch Statement in Java
Java source code in Recursive function for X to the power Y
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Program: Palindrome Test Java Source Code
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Sort Numbers in Ascending Order
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
What is a Java Scanner Class and How to Use it?
What is MDAS? Sample Java Source Code for MDAS