NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96
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.
Unlocked Samsung i777 Galaxy S 2 II AT&T Android 16GB Black Camera Cell Phone
Current Bid: $179.95
Program Algorithm:
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 Source Code for Static Method
//main class
package static_method_in_java;
public class Main {
public static void main(String[] args) {
Static_Method.sample_static_method();
}
}
//java class
package static_method_in_java;
public class Static_Method {
public static void sample_static_method()
{
System.out.println(“This is how Static Method works. Simple isn’t it? :)”);
System.out.println(“Aisha91 at your Service. Bow! LoL, just kidding.”);
}
}
Sample Output:
Related Java Tutorials:
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples
Java Video Tutorials
Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99
Java Book
Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, Sierra Kathy, Bates Be
Current Bid: $19.14
Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99
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