static Method and variable in java, how to access methods and variables

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

1047

Program:

 /*
 * Here we will learn to access Static method and Static Variable.
 */
public class JavaStaticExample {
 
 static int i = 10;
 
 static void method() {
 System.out.println("Inside Static method");
 }
 
 public static void main(String[] args) {
 
 // Accessing Static method
 JavaStaticExample.method();
 
 // Accessing Static Variable
 System.out.println(JavaStaticExample.i);
 
 /*
 * No Instance is required to access Static Variable or Method as we
 * have seen above. Still we can access the same static variable and
 * static method using Instace references as below.
 */
 JavaStaticExample obj1 = new JavaStaticExample();
 JavaStaticExample obj2 = new JavaStaticExample();
 
 /*
 * Accessing static variable in Non Static way. Compiler will warn you
 * with below warning.
 *
 * The static field JavaStaticExample.i should be accessed in a static
 * way.
 */
 System.out.println(obj1.i);
 // Accessing satic method using reference.
 // Warning by compiler
 // "The static method method() from the type JavaStaticExample should be accessed in a static way"
 obj1.method();
 }
}

Output:

Inside Static method
10
10
Inside Static method
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.