Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Main { public static void main(String[] args) { welcomeToJava(); print(); } public static void welcomeToJava() { System.out.println("Welcome To Java Programming"); } public static void print() { welcomeToJava(); welcomeToJava(); } } |
Output:
1 2 3 4 5 | elcome To Java Programming Welcome To Java Programming Welcome To Java Programming |
It is a method which belongs to the class and not to the object(instance) A static method can access only static data. It can not access non-static data (instance variables).
Static methods in Java can be called without creating an object of class.
The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static variable gets memory only once in class area at the time of class loading.