What is a static method? What is a static variable?This article is about "static" keyword usage in java and is divided into two main sections: static variables and static methods:
Static Variables:
A static variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static variables i.e. there is only one copy per class, no matter how many objects are created from it. Class variables or static variables are declared with the
static keyword in a class. These are declared outside a class and stored in static memory. Class variables are mostly used for constants. Static variables are always called by the class name. This variable is created when the program starts and gets destroyed when the programs stops. The scope of the class variable is same an instance variable. Its initial value is same as instance variable and gets a default value when its not initialized corresponding to the data type. Similarly, a static method is a method that belongs to the class rather than any object of the class and doesn't apply to an object or even require that any objects of the class have been instantiated.
Non-static variables take on unique values with each object instance.
You can define class variables as static members, as you knew static variables are shared between all the instances belonging to the same class. You can define class static variables using all the access providers :private, protected, public and default. Below is a class member static variable example where a new instances is create from class Animal and at the constructor of this class the static variable is incremented. At the end of main function the content of the static variable "numberOfAnimals" will be equal to the number of Animal class objects. See below:
- Code:
public class StaticUsageExample {
public static void main(String[] args) {
Animal a1=new Animal();
Animal a2=new Animal();
a1.printNumberOfAnimals();
a2.printNumberOfAnimals();
Animal a3=new Animal();
a1.printNumberOfAnimals();
a3.printNumberOfAnimals();
}
}
class Animal
{
private static int numberOfAnimals;
public Animal() {
numberOfAnimals++;
}
public void printNumberOfAnimals()
{
System.out.println("Number of animals static counter ="+numberOfAnimals);
}
}
The output of this snippet is :
- Code:
Number of animals static counter =2
Number of animals static counter =2
Number of animals static counter =3
Number of animals static counter =3
You don't to create any instances of the class Animal to have access to its static variables, you can just using the class name as follows:
- Code:
public class StaticUsageExample {
public static void main(String[] args) {
System.out.println("Number of animals in zoo is: "+Animal.numberOfAnimals);
}
}
class Animal
{
public static int numberOfAnimals=4;
}
Remember: The static variables are created at start time.
Static methods:
Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a
superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a non-static method. In other words, you can't change a static method into an instance method in a subclass.
The most famous static method in java is the main function :
- Code:
public static void main(String[] args) {
}
It has to be static because It is the entrance of your java application. You can create your one static methods easily as follows :
- Code:
public class StaticUsageExample {
public static void main(String[] args) {
Calculator.sum(5, 4);
}
}
class Calculator
{
public static void sum(int x,int y)
{
System.out.println("The sum is:"+(x+y));
}
}
Please note that you can't use non static class members (variables and methods) inside a static method, following codes give a compile error :
- Code:
class Calculator
{
private int factor=4;
public static void sum(int x,int y)
{
System.out.println("The sum is:"+(x+y)*factor);
}
}
or
- Code:
class Calculator
{
public static void sum(int x,int y)
{
printValues(x+y));
}
public void printValues(int z)
{
System.out.println(z);
}
}
You even can't use the
this. keyword inside a static function.