Java static Keyword

The Java static keyword is used to declare class-level variables and methods that can be accessed without creating an instance of the class. In this tutorial, we will discuss the basics of the Java static keyword and how to use it.

Syntax of Static Keyword:

The static keyword is used to declare class-level variables and methods. Here is the syntax of the static keyword:

public class ClassName{ 
  static int variableName; 
  static void methodName(){ 
    // code to be executed 
  } 
}

In the above example, we have declared a static variable named variableName and a static method named methodName. The static keyword is used to declare these class-level members.

Accessing a Static Variable:

A static variable can be accessed by using the class name followed by the variable name. Here is an example of how to access a static variable:

ClassName.variableName;

In this example, we are accessing the static variable variableName of the ClassName class.

Accessing a Static Method:

A static method can be accessed by using the class name followed by the method name. Here is an example of how to access a static method:

ClassName.methodName();

In this example, we are calling the static method methodName of the ClassName class.

Static vs Non-Static:

The main difference between static and non-static members is that static members are shared among all instances of the class, while non-static members are unique to each instance of the class.

Example:

public class Person{ 
  static int numberOfPeople; 
  String name;
  public Person(String n){
    name = n;
    numberOfPeople++;
  }
}

In the above example, we have defined a class called Person with a static variable named numberOfPeople and a non-static variable named name. We have also defined a constructor for the Person class that sets the name variable and increments the numberOfPeople static variable.

In this example, the numberOfPeople static variable is shared among all instances of the Person class. Each time a new Person object is created, the constructor is called and the numberOfPeople static variable is incremented.

Complete code example on Java static keyword:

Here is an example code snippet that demonstrates the use of the Java static keyword:

/**
 * Java static keyword example
 * 
 * @author Mamun Kayum
 *
 */
public class StaticKeyword {

	// static variable declaration and initialization
	static int numCars = 0;
	String make;
	String model;

	/**
	 * StaticKeyword constructor
	 * 
	 * @param make
	 * @param model
	 */
	public StaticKeyword(String make, String model) {
		this.make = make;
		this.model = model;
		// incrementing static variable without using this keyword
		numCars++;
	}

	/**
	 * static method
	 * 
	 * @return numCars
	 */
	public static int getNumCars() {
		return numCars;
	}

	/**
	 * non-static method
	 * 
	 * @return make
	 */
	public String getMake() {
		return make;
	}

	/**
	 * non-static method
	 * 
	 * @return model
	 */
	public String getModel() {
		return model;
	}

	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		StaticKeyword car1 = new StaticKeyword("Toyota", "Camry");
		StaticKeyword car2 = new StaticKeyword("Honda", "Civic");
		// accessing static method
		System.out.println("Number of cars created: " + StaticKeyword.getNumCars());

		System.out.println("Car 1: " + car1.getMake() + " " + car1.getModel());
		System.out.println("Car 2: " + car2.getMake() + " " + car2.getModel());
	}
}

In this example, we have a Car class with a static variable numCars that keeps track of the number of Car objects created. The Car constructor increments the numCars static variable each time a new Car object is created.

We also have a static method getNumCars() that returns the current value of the numCars static variable. The getMake() and getModel() methods are non-static and return the make and model instance variables of a Car object.

In the main() method, we create two Car objects and print out their make and model. We also print out the total number of Car objects created using the getNumCars() static method.

When we run this code, we get the following output:

Number of cars created: 2
Car 1: Toyota Camry
Car 2: Honda Civic

As you can see, the numCars static variable is shared among all instances of the Car class, and the getNumCars() static method can be called without creating a Car object.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author