Static Keyword in Java with Examples

In this tutorial, we will learn about static keyword in Java and its uses. We will also cover examples and best practices for using static variables and methods in the Java code.


Java is a popular, object-oriented programming language that is widely used in software development. One of the key features of Java is the use of the static keyword, which can be applied to variables and methods. Static keyword in Java is used to indicate that a variable or method is a class-level variable or method. This means that the variable or method is associated with the class itself, rather than with any particular object that is created from the class.


One common use of static variables is for defining class-level constants. For example, in a class that represents a mathematical circle, we might define a static variable for the value of pi as follows:

java
public class Circle {
    public static final double PI = 3.14;
}

In this example, the variable PI is defined as a static, final variable, which means that it is a class-level constant that cannot be changed. We can then access this variable from anywhere in our code by referencing the class name, as in Circle.PI.


Another common use of static variables is for keeping track of information that is shared across all objects of a class. For example, in a class that represents a bank account, we might define a static variable for the next account number as follows:

java
public class BankAccount {
    private static int nextAccountNumber = 1001;
    private int accountNumber;
    public BankAccount() {
        accountNumber = nextAccountNumber++;
    }
}

In this example, the variable nextAccountNumber is defined as a private, static variable. Each time a new BankAccount object is created, the constructor assigns the nextAccountNumber variable to the accountNumber variable and increases the nextAccountNumber value by 1.


Similarly, static methods are class-level methods that can be accessed without creating an object of the class. They are often used for utility methods that perform actions that are not specific to any particular object. For example, we might define a static method in our Circle class for calculating the circumference of a circle as follows:

java
public class Circle {
    public static final double PI = 3.14;
    public static double circumference(double radius) {
        return 2 * PI * radius;
    }
}

In this example, the method circumference is defined as a static method. It can be called by referencing the class name, as in Circle.circumference(5).


It's important to note that static variables and methods can be accessed without creating an object of the class and they are also shared among all objects of the class. Also, It is always better to make the static variables as private, as they are class-level variables and should not be accessible outside the class.


In conclusion, the static keyword in Java is a powerful tool that allows us to define class-level variables and methods. It is used for defining class-level constants, shared information, and utility methods that do not depend on the state of any particular object. When used correctly, it can improve the organization and performance of your code.


However, it is important to be mindful of the implications of using static variables and methods. For example, if a static variable is shared among multiple objects, changes made to that variable by one object will be visible to all other objects, which can lead to unexpected behavior. It is also important to be aware that static methods can not access non-static variables and methods of the class.


In general, it is best to use static variables and methods only when necessary, and to make them private to maintain encapsulation and avoid unexpected behavior. It is also a good practice to use static import when we need to use static methods or variables frequently.