Static Members
By the Syncfusion DevTutorials team · Last updated
Share data and behaviour across a whole class with static variables, static methods and static blocks, and learn why main is static.
Think about a classroom.
Every student has their own notebook. What Asha writes in hers does not appear in Ravi's. But the classroom has one whiteboard on the wall. Everyone sees the same whiteboard, and when the teacher writes on it, every student sees the change.
In a Java class, normal fields are like the notebooks. Each object has its own. A static field is like the whiteboard. There is only one, and every object shares it.
In simple words: a static member belongs to the class itself, not to any one object.
What You Will Learn
In this lesson, you will learn:
- What static members are, and how they differ from instance members.
- How static variables share one value across all objects.
- How to write and call static methods.
- How static blocks set up a class once.
- Why
main()must be static. - How to access static members correctly.
- When static members are the right choice.
Start With a Working Example
Press Run.
One shared counter, three separate names
public class Main {
public static void main(String[] args) {
Visitor a = new Visitor("Asha");
Visitor b = new Visitor("Ravi");
Visitor c = new Visitor("Meena");
System.out.println(a.name + ", " + b.name + ", " + c.name);
System.out.println("Total visitors: " + Visitor.count);
}
}
class Visitor {
static int count = 0;
String name;
Visitor(String name) {
this.name = name;
count++;
}
}
Expected output:
Asha, Ravi, Meena
Total visitors: 3
Each visitor has their own name. But there is only one count, and every constructor adds to it. After three objects, it holds 3.
Try This
Create a fourth visitor. The total goes up to 4, without any change to the Visitor class.
Introduction to Static Members
A member of a class is any field or method inside it.
Members come in two kinds:
- Instance members belong to each object. Every object gets its own copy of instance fields.
- Static members belong to the class. There is exactly one copy, shared by all objects.
You make a member static by adding the static keyword.
class Student {
static String school = "City School"; // one for the whole class
String name; // one per object
}
Static members exist as soon as the class is loaded. You do not need to create any object to use them.
Static Variables
A static variable (also called a class variable) has one value, shared by every object of the class.
If any code changes it, every object sees the new value.
Change a static field once, every object sees it
public class Main {
public static void main(String[] args) {
Employee a = new Employee("Asha", 40000);
Employee b = new Employee("Ravi", 55000);
a.showPay();
b.showPay();
Employee.bonusPercent = 20;
System.out.println("-- bonus raised to 20% --");
a.showPay();
b.showPay();
}
}
class Employee {
static int bonusPercent = 10;
String name;
int salary;
Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
void showPay() {
int bonus = salary * bonusPercent / 100;
System.out.println(name + " gets a bonus of " + bonus);
}
}
Expected output:
Asha gets a bonus of 4000
Ravi gets a bonus of 5500
-- bonus raised to 20% --
Asha gets a bonus of 8000
Ravi gets a bonus of 11000
bonusPercent was changed once. Both employees used the new rate straight away, because there is only one bonusPercent.
salary and name are instance fields, so each employee keeps their own.
Static Constants
A static field marked final is a constant that belongs to the class. This is the standard way to write constants in Java.
class Circle {
static final double PI = 3.14159;
}
Math.PI and Integer.MAX_VALUE are static constants from Java's own classes.
Static Methods
A static method belongs to the class. You call it with the class name, without creating an object.
ClassName.methodName(arguments)
You have already used many: Math.max(), Integer.parseInt(), String.valueOf() and Arrays.sort().
Static methods suit jobs that depend only on their parameters, like calculations and conversions.
Static methods need no object
public class Main {
public static void main(String[] args) {
System.out.println(TempConverter.toFahrenheit(37));
System.out.println(TempConverter.toCelsius(212));
System.out.println(TempConverter.isFever(38.5));
}
}
class TempConverter {
static double toFahrenheit(double celsius) {
return celsius * 9 / 5 + 32;
}
static double toCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
static boolean isFever(double celsius) {
return celsius >= 38;
}
}
Expected output:
98.6
100.0
true
No TempConverter object was ever created. Each method only uses its own parameter.
What a Static Method Cannot Do
A static method has no object, so there is no this.
That means a static method cannot use instance fields or call instance methods directly. Which object's field would it use?
A static method cannot reach an instance field
public class Main {
public static void main(String[] args) {
System.out.println(Counter.describe());
}
}
class Counter {
int value = 5;
static String describe() {
return "Value is " + value;
}
}
The compiler reports non-static variable value cannot be referenced from a static context.
An instance method can use static members, though. Every object can see the shared class data.
| Can it use... | From a static method | From an instance method |
|---|---|---|
| Static fields and methods | Yes | Yes |
| Instance fields and methods | No (only through an object) | Yes |
this | No | Yes |
Static Blocks
A static block is a block of code that runs once, when the class is first loaded. It runs before any object is created and before any static method is called.
You write it with the static keyword and curly braces:
static {
// set up static fields here
}
Use a static block when a static field needs more than one line to set up, such as filling a table of values.
A static block runs once, before anything else in the class
public class Main {
public static void main(String[] args) {
System.out.println("main starts");
System.out.println("Tax for food: " + TaxTable.rateFor(0) + "%");
System.out.println("Tax for phones: " + TaxTable.rateFor(2) + "%");
}
}
class TaxTable {
static int[] rates = new int[3];
static {
System.out.println("Loading tax table...");
rates[0] = 5;
rates[1] = 12;
rates[2] = 18;
}
static int rateFor(int category) {
return rates[category];
}
}
Expected output:
main starts
Loading tax table...
Tax for food: 5%
Tax for phones: 18%
The static block ran when TaxTable was first used, not at the start of the program. And it ran only once, even though rateFor() was called twice.
Java loads a class the first time it is needed, for example when you first create an object or use a static member. Static fields get their values, and static blocks run, at that moment, in the order they appear in the class.
Why main() is static
public static void main(String[] args)
When you start a program, no objects exist yet. Java has to call some method to begin.
If main were an instance method, Java would first need an object of the Main class. But to create that object, it would need to run some code first. That is a chicken-and-egg problem.
Making main static solves it. The JVM can call Main.main(args) straight away, using only the class name.
This is also why main cannot call your instance methods directly. To use them, main first creates an object:
main creates an object to call instance methods
public class Main {
int lessonsDone = 2;
void showProgress() {
System.out.println("Lessons done: " + lessonsDone);
}
static void welcome() {
System.out.println("Welcome back!");
}
public static void main(String[] args) {
welcome();
Main app = new Main();
app.showProgress();
}
}
Expected output:
Welcome back!
Lessons done: 2
welcome() is static, so main calls it directly. showProgress() is an instance method, so main creates a Main object first.
A static field that changes is shared by the whole program. Any code can change it, at any time, which can make bugs hard to trace. Prefer static final constants, and keep changing static fields private.
Static Member Access
From Outside the Class
Use the class name and a dot: Employee.bonusPercent, Math.sqrt(16).
From Inside the Same Class
You can use the name on its own: count++, rateFor(2).
Through an Object
Java also lets you write a.count when a is an object. It works, but it is misleading. It looks as if each object has its own count, which is not true.
Same static field, reached three ways
public class Main {
public static void main(String[] args) {
Score.total = 10;
Score s = new Score();
s.total = 25; // works, but looks like it belongs to s
System.out.println(Score.total);
System.out.println(s.total);
System.out.println(new Score().total);
}
}
class Score {
static int total;
}
Expected output:
25
25
25
All three lines read the same single field. Changing s.total changed it for everyone.
Always use the class name for static members. It makes it clear that the value is shared.
Static Imports
import static lets you use another class's static members without writing the class name.
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
double r = sqrt(PI);
Use it sparingly. Without the class name, readers may not know where a method comes from.
Static vs Instance Members
| Static member | Instance member | |
|---|---|---|
| Keyword | static | none |
| Belongs to | The class | Each object |
| Number of copies | One, shared | One per object |
| Exists when | The class is loaded | An object is created with new |
| Accessed with | ClassName.member | object.member |
Can use this | No | Yes |
| Example | Math.PI, Visitor.count | student.name, cart.total |
Easy Way to Remember: ask "does this value differ from object to object?" If yes, make it an instance member. If it is the same for everyone, make it static.
Use Cases of Static Members
Counters Shared by All Objects
Counting how many objects were created, like the visitor counter at the start.
Constants
Values that never change and are the same for everyone: static final int MAX_USERS = 100;.
Utility Methods
Methods that only use their parameters, like Math.max() or a TempConverter. A class made only of static methods is called a utility class.
Factory Methods
A static method that creates and returns objects, like LocalDate.of(2026, 1, 1) or List.of(1, 2, 3). It can give the creation a clear name.
Unique IDs
A static counter can hand out a new ID to every object.
A static counter gives each order its own ID
public class Main {
public static void main(String[] args) {
Order a = new Order("Pens");
Order b = new Order("Bag");
Order c = new Order("Shoes");
a.show();
b.show();
c.show();
}
}
class Order {
private static int nextId = 1001;
private final int id;
private final String item;
Order(String item) {
this.id = nextId;
nextId++;
this.item = item;
}
void show() {
System.out.println("Order #" + id + ": " + item);
}
}
Expected output:
Order #1001: Pens
Order #1002: Bag
Order #1003: Shoes
nextId is shared, so each new order picks up the next number. id is an instance field, so each order keeps its own number.
When Not to Use Static
Do not make something static just to avoid creating objects. Data that belongs to one object, such as a student's name or an account's balance, must be an instance field. If it were static, every student would share one name.
Common mistakes
The following mistakes are common when using static fields and methods. Each row shows what you see, why it happens and how to fix it.
| Symptom | Cause | Fix |
|---|---|---|
non-static method cannot be referenced from a static context | main called an instance method directly | Create an object, or make the method static |
non-static variable cannot be referenced from a static context | A static method used an instance field | Pass the value in, or use an object |
| Every object shows the same name | A per-object field was made static | Remove static from that field |
| A "constant" changes during the program | It was not marked final | Use static final |
| Code is hard to read: is this shared? | A static field was used through an object | Use the class name |
What to Remember
- A static member belongs to the class. There is one copy, shared by every object.
- Static variables hold shared data.
static finalfields are class constants. - Static methods are called with the class name and cannot use
thisor instance fields directly. - A static block runs once, when the class is first loaded.
mainis static so the JVM can start the program before any object exists.- Use static for shared values, constants, utility methods and ID counters. Keep per-object data as instance members.
Next: Advanced Classes