Your language · Java
Keywords, access and scope
static, final and access modifiers, or scope rules and naming conventions.
Learn: static, final, this, super and access modifiers in Java8 minute read
A few Java keywords decide three things about every field and method: who can see it, whether it belongs to the class or to each object, and whether it can change. They come up in almost every Java interview, often as "what does this print?" puzzles about static blocks and constructors. They also matter in test code: a static driver field (the object that controls the browser, shared by the whole class) is a classic reason why parallel tests (tests that run at the same time) break. A field is a variable inside a class; a method is a named block of code inside a class.
Step 1: access modifiers
| Modifier | Class | Package (a group of related classes) | Subclass (a class that extends this one) elsewhere | Everyone |
|---|---|---|---|---|
| private | Yes | No | No | No |
| (none) package-private | Yes | Yes | No | No |
| protected | Yes | Yes | Yes | No |
| public | Yes | Yes | Yes | Yes |
A top-level class can only be public or package-private (no keyword), and a file holds at most one public top-level class, named like the file. In a framework, locators (the rules for finding elements on a page) are private, the driver in a base class is protected so subclasses in other packages can use it, and page actions are public. An overriding method may widen access but never narrow it.
Step 2: static belongs to the class
- A static field has one copy shared by every object, such as a counter or a constant.
- A static method is called on the class and has no
this, so it cannot use instance fields without an object. - A static block runs once, when the class is first set up. It is a common place to load a properties file (a text file of settings).
Be careful with static state in tests. A static WebDriver is shared by every test in the JVM (the program that runs Java code), so in a parallel run tests drive each other's browsers. A ThreadLocal<WebDriver>, which gives each thread its own copy, fixes this.
Step 3: the order things run in
The first time a class is used, its static blocks run, parent before child. An instance block is a plain { ... } in the class body that runs for every new object, just before the constructor body. So for every new, the parent's instance blocks and constructor run, followed by the child's.
class Base {
static { System.out.println("Base static block"); }
{ System.out.println("Base instance block"); }
Base() { System.out.println("Base constructor"); }
}
class Child extends Base {
static int created = 0;
static { System.out.println("Child static block"); }
{ System.out.println("Child instance block"); }
Child() {
super();
created++;
System.out.println("Child constructor");
}
}
public class Main {
public static void main(String[] args) {
new Child();
System.out.println("---");
new Child();
System.out.println("objects created: " + Child.created);
}
}
Base static block Child static block Base instance block Base constructor Child instance block Child constructor --- Base instance block Base constructor Child instance block Child constructor objects created: 2
The second object skips both static blocks, and the static counter shows that both objects share one created field.
Step 4: final means "cannot change"
- A final variable is assigned once. For a reference, final fixes which object the variable points at, not the object itself: a
final Listcan still get new items. - A final method cannot be overridden, and a final class cannot be extended;
Stringis final. static finalfields are constants, named in capitals:static final Duration TIMEOUT = Duration.ofSeconds(10);
import java.util.*;
public class Main {
static final int MAX_RETRIES = 3;
public static void main(String[] args) {
final List<String> tags = new ArrayList<>();
tags.add("smoke"); // the list can change
// tags = new ArrayList<>(); // would not compile: tags is final
System.out.println(tags + " " + MAX_RETRIES);
}
}
[smoke] 3
Do not mix final up with finally, the clean-up block after try (see Exceptions), or finalize(), an old Object method that has been deprecated for removal since Java 18.
Step 5: this, super and scope
this is the current object. It tells a field from a parameter with the same name (this.driver = driver;) and lets a method return the object so calls can be chained (page.typeUser("a").typePassword("b")). super reaches the parent: super.method() runs the parent's version, and super(...) calls a parent constructor. Neither works in a static method.
public class LoginPage extends BasePage {
public LoginPage(WebDriver driver) {
super(driver); // BasePage sets up driver and wait
}
public LoginPage typeUser(String user) {
type(USERNAME, user);
return this; // fluent chaining
}
}
A variable is visible from where it is declared to the end of its block { }. A lambda (a short piece of code written as x -> ...; see Lambdas and streams) may read a local variable only if that variable is final or never changed after it is set ("effectively final").
How to answer in an interview
Tie each keyword to a test framework use. "Locators are private, the driver in BaseTest is protected, page actions are public. Config is loaded once in a static block. Timeouts are static final constants. I avoid a static driver because it breaks parallel runs." For an order puzzle, say the rule first: static blocks once, parent first; then instance blocks and constructors, parent first, for every object.
Common mistakes
- Making the WebDriver
staticand then running tests in parallel (at the same time). - Thinking a
finallist cannot change. - Expecting static blocks to run again for every object.
- Using
thisor instance fields inside a static method. - Confusing
final,finallyandfinalize.
Recap
Access goes from private (class only) to package-private, protected and public. static members belong to the class and static blocks run once. final locks a variable, a method or a class. this is the current object and super the parent part. Construction runs parent before child.
8 questions0/8 solved
| Status | Question | Difficulty | Asked in |
|---|---|---|---|
| Not started | Who can see a protected member? | Easy | Asked in: Accenture, Capgemini, Infosys +1 |
| Not started | Access modifiers: private, default, protected, public | Easy-Medium | Asked in: Accenture, Capgemini, Infosys +1 |
| Pro+ | The static keyword: variables, methods, blocks and run order | Medium | Asked in: Accenture, Infosys, TCS +1 |
| Pro+ | final vs finally vs finalize | Easy-Medium | Asked in: Accenture, Capgemini, Infosys +1 |
| Pro+ | Order of static blocks, instance blocks and constructors | Medium | Asked in: Accenture, Infosys, TCS +1 |
| Pro+ | What does final stop on a list variable? | Easy-Medium | Asked in: Accenture, Capgemini, Infosys +1 |
| Pro+ | What can a static method use? | Medium | Asked in: Accenture, Infosys, TCS +1 |
| Pro+ | Which modifiers can a top-level class have? | Easy-Medium | Asked in: Accenture, Capgemini, Infosys +1 |
The first 2 questions of each topic are free. Pro+ opens every question.