Objective

On this section we will be dealing with static method and variables. The keyword static is one of the modifiers of Java language that is useful when you want to have your variables that is common to all objects.  Maybe you have already encountered the term static fields or class variables. Well, they are simply fields that have static declaration in their declaration.

Static Variable

Below are some important notes on static variables

  • Static variables are associated with the class which means it does not belong to any Object
  • Each instance of the class shares a class variable which is one fixed location in memory thus any object can change the value of the class variable
  • Even though any object can change the value of the class variable, a class variable can be changed or manipulated also without creating an instance of the class.

Static Method

Below are some important notes on static method

  • Static variables are associated with the class which means it does not belong to any Object
  • Each instance of the class shares a class variable which is one fixed location in memory thus any object can change the value of the class variable
  • Even though any object can change the value of the class variable, a class variable can be changed or manipulated also without creating an instance of the class.

Static Variable and Method Example

package com.javatutorialhq.java.staticexamples;

/*
 * Sample Class with static variable and method
 */

public class StaticExample {

	static String variable1 = "test string";
	String variable2 = "non-static variable";
	
		
	
	public static String methodStatic (){
		/*
		 * Since this is a static method 
		 * we can access static variable
		 * without specifying class name
		 */
		return variable1;
	}
	
	public String methodNonStatic(){
		
		String val = variable1 + " " + variable2;
		return val;
		
	}
	
	
}
package com.javatutorialhq.java.staticexamples;

/*
 * This is an example on the how to use static
 * variables and methods
 */

public class Main {

	public static void main(String[] args) {
		
		
		/*
		 * access the static variable variable1
		 * format is ClassName.
		 */
		System.out.println("*****Static Variable Examples ******");
		System.out.println(StaticExample.variable1);
		
		/*
		 * access the non static variable variable2
		 * take note that we have to create a new object to do this
		 */
		StaticExample obj = new StaticExample();
		
		//System.out.println(obj.variable1); /* wrong */
		System.out.println(obj.variable2); /* right way */
		
		
		
		System.out.println("*****Static Method Examples ******");

		/*
		 * call the static method
		 */
		
		System.out.println(StaticExample.methodStatic());
		
		/*
		 * access non static method
		 */
		System.out.println(obj.methodNonStatic());
		
		
		
	}

}

Sample Output

If you run the above example, we would be having the following output

*****Static Variable Examples ******
test string
non-static variable
*****Static Method Examples ******
test string
test string non-static variable