Java Static
Java static Keyword
In the previous chapter, you learned a little about the static keyword when working with class attributes and methods.
Now, let's go more in depth and see how static really works.
Static makes attributes and methods belong to the class instead of the objects, which means they are shared by all objects.
Static Attributes
If you create multiple objects of one class, the attributes normally have different values. But if you declare an attribute as static, all objects share the same value.
In this example, both objects share the same value of the static attribute x:
Example
public class Main {
static int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main();
Main myObj2 = new Main();
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Also outputs 5
}
}
If one object changes the value of x, the change is seen by the other object too:
Example
public class Main {
static int x = 0;
public static void main(String[] args) {
Main myObj1 = new Main();
Main myObj2 = new Main();
myObj1.x = 7; // Change x using myObj1
System.out.println(myObj1.x); // Outputs 7
System.out.println(myObj2.x); // Also outputs 7
}
}
Explanation:
Because x belongs to the class, changing it through one object also changes it for all objects.
Normal Attributes
To see the difference, here is the same example but without static.
Now each object has its own copy of x:
Example
public class Main {
int x = 5; // Normal attribute
public static void main(String[] args) {
Main myObj1 = new Main();
Main myObj2 = new Main();
myObj1.x = 9; // Change x in myObj1 only
System.out.println(myObj1.x); // 9
System.out.println(myObj2.x); // 5 (unchanged)
}
}
Explanation: Normal attributes belong to each object separately. Changing the value in one object does not affect the others.
Static Methods
Like static attributes, static methods also belong to the class. That means you can call them without creating an object first:
Example
public class Main {
static void myStaticMethod() {
System.out.println("Static methods can be called without objects");
}
public static void main(String[] args) {
myStaticMethod(); // Call the static method
}
}
Tip: Static methods are often used for utility methods - small helper methods that can be reused, like doing math calculations or formatting text, without needing to create an object first.
Here we create our own utility method, square(), to calculate the square of a number:
Example
public class Main {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(square(5)); // 25
}
}
Static Methods and Normal Attributes
A static method cannot use normal attributes
directly, because normal attributes belong to objects, while static methods
belong to the class:
Example
This will cause an error, because x is not static:
public class Main {
int x = 10;
static void myStaticMethod() {
System.out.println(x); // Error: cannot use x here
System.out.println("Static methods cannot use normal attributes directly");
}
public static void main(String[] args) {
myStaticMethod();
}
}
Fix: To use a normal attribute inside a static method, create an object first:
Static Methods and this
The this keyword refers to the current object. But static methods belong to the class, not to objects. That means you cannot use this inside a static method.
Example
Using this in a static method will cause an error:
public class Main {
int x = 10;
static void myStaticMethod() {
System.out.println(this.x); // Error
System.out.println("Static methods do not have 'this'");
}
public static void main(String[] args) {
myStaticMethod();
}
}
Normal Methods with Static Attributes
Normal methods can use static attributes, because static attributes belong to the class and are always available.
Example
Both objects share the same x value:
public class Main {
static int x = 0;
public void addOne() {
x++;
}
public static void main(String[] args) {
Main myObj1 = new Main();
Main myObj2 = new Main();
myObj1.addOne();
myObj2.addOne();
System.out.println("Value of x: " + Main.x); // Outputs 2
}
}
Why main() is Static
The main() method is static so that the program can run without creating an object first. Java needs to start running the program before any objects are made.
Summary
staticattributes and methods belong to the class, not objects.- Static attributes are shared by all objects of the class.
- Static methods can be called without creating objects.
- Static methods cannot use normal attributes directly.
- Normal methods can use static attributes.
- Static methods cannot use
this. - The
main()method is static because the program must run before objects are created.