📓3.7: Class (static) Variables & Methods
Table of Contents
📖 This page is a condensed version of CSAwesome Topic 3.7
In Java, the keyword static
means a variable or method belongs to the class rather than to individual objects.
Static Variables
- Shared by all objects of the class.
- Only one copy exists, regardless of how many objects are created.
- Can be used for constants or counters.
Example:
public class Student {
private String name;
private static int studentCount = 0;
public Student(String n) {
name = n;
studentCount++;
}
public static int getStudentCount() {
return studentCount;
}
}
Usage:
Student s1 = new Student("Alex");
Student s2 = new Student("Jordan");
System.out.println(Student.getStudentCount()); // prints 2
Static Methods
- Can be called without creating an object.
- Cannot access instance variables directly (since they belong to objects, not the class).
- Often used for utility methods.
Example:
public class MathHelper {
public static int square(int num) {
return num * num;
}
}
Usage:
int result = MathHelper.square(5); // returns 25
Summary
- Static variables: Shared data for all objects of the class.
- Static methods: Called on the class itself, not an object.
- Useful for constants, counters, and utility methods.