By Shivam Singh, founder of TesterRank · Updated September 2026
static means "belongs to the class, not to any object". final means "cannot be changed". These are used extensively in automation for constants, utility methods, and configuration.
static means the value belongs to the class itself, not to each object. All objects share it, which suits settings like a base URL.
public class Config {
// static fields — shared across all instances
static String BASE_URL = "https://demo-bank.example.com";
static int TIMEOUT = 30;
static String BROWSER = "chrome";
// static method — call without creating an object
static String getLoginUrl() {
return BASE_URL + "/login";
}
static String getApiUrl(String endpoint) {
return BASE_URL + "/api/" + endpoint;
}
}
public class StaticDemo {
public static void main(String[] args) {
// Access without creating an object
System.out.println(Config.BASE_URL);
System.out.println(Config.getLoginUrl());
System.out.println(Config.getApiUrl("users"));
}
}final means the value cannot change after it is set. Use it for constants such as timeouts.
public class FinalDemo {
// final variable — value cannot change
static final String ADMIN_USER = "admin@test.com";
static final int MAX_RETRIES = 3;
// final method — cannot be overridden by subclasses
final void logStep(String step) {
System.out.println("[STEP] " + step);
}
public static void main(String[] args) {
System.out.println(ADMIN_USER);
// ADMIN_USER = "other"; // Compile error: cannot assign to final
}
}
// final class — cannot be extended
final class Utility {
static int add(int a, int b) { return a + b; }
}
// class MyUtil extends Utility { } // Compile error: cannot extend final class| final on | Effect | Example |
|---|---|---|
| Variable | Value cannot change | final int MAX = 10; |
| Method | Cannot be overridden | final void close() { } |
| Class | Cannot be extended | final class String { } |
Q: What is the difference between static and final?
A: static means "belongs to the class" — shared across all instances, accessed via ClassName.field. final means "cannot be changed" — the value/behavior is locked. They serve different purposes and are often used together: static final creates a class-level constant like static final String BASE_URL = "...". static alone allows changes; final alone allows per-instance constants.
Exercise 1: Create a TestCounter class with a static field count that tracks how many test objects have been created. Increment it in the constructor. Create 5 TestCounter objects and print the final count.
Exercise 2: Create a Constants class with static final fields for BASE_URL, TIMEOUT, ADMIN_EMAIL, and ADMIN_PASSWORD. Use them from another class without creating an object.
Answer all 10 questions, then submit to see your score.
1. What keyword creates an object from a class?
2. What does a constructor do?
3. What access modifier makes a field accessible only within its own class?
4. What keyword does a child class use to extend a parent class?
5. What is the output? Shape s = new Circle(); s.area(); — if Circle overrides area()
6. Can a class implement multiple interfaces?
7. What does the static keyword mean?
8. What does super() do in a constructor?
9. Can you instantiate an abstract class directly?
10. What does final on a variable mean?