Java Static Variables — Race Condition in Trade Counter
Duplicate sequence numbers and fluctuating trade counts from unsynchronized static variables.
20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.
- ✓Basic programming fundamentals
- ✓A computer with internet access
- ✓Willingness to follow along with examples
- Variables are named memory slots with a type and value
- Constants use
finalkeyword and SCREAMING_SNAKE_CASE - Local variables must be initialized before use; instance/static get defaults
- Java has 8 primitive types plus reference types for objects
- Use
finalfor values that must never change — compiler enforces it - Uninitialized local variables cause a compile-time error
Java static variables (also called class variables) exist exactly once per class, not per instance. When you declare a field with static, that variable belongs to the class itself — all instances share the same memory location. This is fundamentally different from instance variables, where each object gets its own copy.
The primary reason static variables exist is to represent state that is truly global to the class: configuration constants, counters shared across all instances, or cached data that shouldn't be duplicated. In production systems, you'll see them used for things like connection pool sizes, application-wide flags, or the infamous static int counter that tracks how many times a method has been called across all threads.
The critical thing to understand — and where most bugs live — is that static variables are not thread-safe by default. A static int tradeCounter incremented with counter++ in a high-frequency trading system will corrupt data under load because the read-modify-write operation isn't atomic.
This is the classic race condition: two threads read the same value (say 42), both increment to 43, and you lose one increment. In practice, you either synchronize access, use AtomicInteger, or avoid mutable static state entirely. Many teams enforce a rule: static variables should be final unless you have a very good reason and a concurrency strategy.
Where static variables fit in the Java memory model: they live in the method area (or heap, depending on JVM version), not on the stack. They are initialized when the class is first loaded, before any instance is created. This means you can access them without creating an object — TradeCounter.totalTrades works even if no TradeCounter object exists.
The alternatives are instance variables (per-object state), local variables (method-scoped), or thread-local storage (ThreadLocal) when you need per-thread state without sharing. Don't use static variables for anything that should be scoped to a request, user session, or transaction — that's what dependency injection frameworks like Spring manage with prototype or request scopes.
Think of a variable like a labelled box in your bedroom. You write a name on the outside — say 'socks' — and you can put things in, take them out, or swap the contents whenever you like. A constant is like a box that's been padlocked shut the moment you fill it — the label says 'date of birth' and nothing can ever change what's inside. Java works exactly the same way: variables hold data that can change, constants hold data that must never change.
| Chrome | Firefox | Safari | Edge |
|---|---|---|---|
| ✓ | ✓ | ✓ | ✓ |
Every program you have ever used — a weather app, a video game, a banking website — is constantly juggling data. A temperature reading, a player's score, a bank balance: all of it has to live somewhere in memory while the program is running. Without a way to name and store that data, writing even the simplest program would be impossible. Variables are the fundamental building blocks that make this work, and they show up in literally every Java file ever written.
Before variables existed, early programmers had to reference raw memory addresses — imagine telling someone to 'grab the thing at shelf 0x3F4A' instead of just saying 'grab the sugar'. Variables solve this by letting you give meaningful names to memory locations, so your code reads like English instead of a hardware manual. Constants take this one step further by letting you shout 'this value must NEVER change' — which prevents bugs that are notoriously hard to track down.
By the end of this article you'll know how to declare every kind of variable Java supports, understand the difference between primitive and reference types, lock down values with the final keyword, name things in a way that won't embarrass you in a code review, and dodge the three most common mistakes beginners make on day one.
What a Variable Actually Is — Memory, Names and Types
When your Java program runs, the operating system hands it a chunk of RAM to work with. A variable is just a named slot inside that RAM. You tell Java three things when you create one: what type of data it will hold, what you want to call it, and optionally what value to put in it right away.
The type matters because Java needs to know how much memory to reserve. A single character takes up far less space than a decimal number with ten digits. Java's 'primitive types' are the basic building blocks — whole numbers, decimal numbers, single characters, and true/false values. Think of them as different sized boxes in your warehouse.
Every variable has a lifecycle. It comes into existence when you declare it, holds whatever value you give it, and is destroyed when the block of code it lives in finishes running. Understanding this — called scope — will save you hours of debugging later.
You can declare a variable without giving it a value (declaration only), or declare it and give it a value at the same time (declaration + initialization). Both are valid, but Java will refuse to let you use a local variable before it's been given a value — it's stricter than most languages and that strictness is actually a gift.
public class VariableBasics { public static void main(String[] args) { // --- DECLARATION ONLY --- // We reserve a slot called 'playerAge' that will hold a whole number (int). // It has no value yet — we cannot READ it until we assign one. int playerAge; // --- DECLARATION + INITIALIZATION --- // We reserve the slot AND immediately put the value 25 inside it. int playerScore = 250; // --- ASSIGNING AFTER DECLARATION --- // Now we fill the slot we created earlier. playerAge = 25; // --- DECIMAL NUMBERS use 'double' --- // 'double' stores numbers with a decimal point (64-bit precision). double itemPrice = 19.99; // --- TRUE/FALSE values use 'boolean' --- // Only two possible values: true or false. Perfect for flags and conditions. boolean isLoggedIn = true; // --- SINGLE CHARACTERS use 'char' --- // Note the SINGLE quotes — double quotes are for Strings, not chars. char grade = 'A'; // --- PRINTING the values to the console --- System.out.println("Player age: " + playerAge); // prints 25 System.out.println("Player score: " + playerScore); // prints 250 System.out.println("Item price: " + itemPrice); // prints 19.99 System.out.println("Logged in: " + isLoggedIn); // prints true System.out.println("Grade: " + grade); // prints A // --- CHANGING a variable's value --- // This is the whole point of a *variable* — it can vary. playerScore = 500; // the old value 250 is gone, replaced by 500 System.out.println("Updated score: " + playerScore); // prints 500 } }
Variable Naming Rules and the Conventions That Get You Hired
Java has hard rules — break them and the code won't compile. Then there are conventions — break those and senior developers will wince during your code review.
The hard rules: A variable name must start with a letter, underscore _, or dollar sign $. It cannot start with a number. It cannot be a Java keyword like int, class, or return. Names are case-sensitive, so playerScore and playerscore are two completely different variables.
The convention: camelCase. Java developers universally use camelCase for variable names — start with a lowercase letter, then capitalize the first letter of every subsequent word. So accountBalance, numberOfLives, isEmailVerified. This isn't enforced by the compiler, but deviating from it signals immediately that you're new to the language.
Name for meaning, not brevity. A variable called n tells the next developer nothing. A variable called numberOfActiveUsers tells them everything. Yes, it's more typing. The five seconds you save typing a short name costs the next person (often future-you) five minutes of head-scratching.
Avoid abbreviations unless they're universally understood (url, id, html are fine). Avoid names that include the type, like intPlayerAge — that's a relic from the 1990s called Hungarian notation and it's considered noise in modern Java.
public class NamingConventions { public static void main(String[] args) { // ✅ GOOD — camelCase, descriptive, instantly readable int numberOfStudentsEnrolled = 42; double annualSalaryInDollars = 75000.00; boolean isAccountSuspended = false; String customerFullName = "Maria Chen"; // String = text, not a primitive // ❌ BAD — compiles fine but will get you flagged in code review int n = 42; // what does 'n' mean? Nobody knows. double d = 75000.00; // is this salary? a measurement? a count? boolean flag = false; // a 'flag' for WHAT? String s = "Maria Chen"; // seriously, just... no. // ❌ ILLEGAL — these will cause a compile error // int 2ndPlayer = 5; // starts with a number — ILLEGAL // int player-score = 5; // hyphens not allowed — ILLEGAL // int class = 5; // 'class' is a reserved keyword — ILLEGAL // Printing the good variables so we can run this System.out.println("Students enrolled: " + numberOfStudentsEnrolled); System.out.println("Annual salary: $" + annualSalaryInDollars); System.out.println("Account suspended: " + isAccountSuspended); System.out.println("Customer name: " + customerFullName); } }
temp in a critical financial calculation masked a bug for three sprints.n is never acceptable.Constants With `final` — Locking Down Values That Must Never Change
Some values in your program should never change after they're set. The number of days in a week. The value of Pi. Your application's maximum upload size. The tax rate for a given region. If these values could accidentally be overwritten, bugs would be catastrophic — imagine a banking app where the interest rate could be silently changed mid-calculation.
Java's answer is the final keyword. Put final before a variable declaration and Java will throw a compile error the moment anyone tries to reassign it. The value is locked in for the lifetime of the program. These are called constants.
By convention, constant names use SCREAMING_SNAKE_CASE — all uppercase letters with underscores separating words. This visual distinction means any developer who reads MAX_LOGIN_ATTEMPTS instantly knows 'this value never changes', without having to scroll up to check the declaration.
You'll most often see constants declared with both static and final at the class level. static means the constant belongs to the class itself rather than to any one object — so there's only ever one copy of it in memory, shared everywhere. For now, just know that static final is the standard idiom for class-level constants and you'll see it constantly in production code.
public class ConstantsDemo { // Class-level constants: static final + SCREAMING_SNAKE_CASE // These are defined outside main() so the whole class can access them. // The maximum number of times a user can attempt to log in before lockout. static final int MAX_LOGIN_ATTEMPTS = 5; // The sales tax rate — defined once, used everywhere in billing calculations. static final double SALES_TAX_RATE = 0.08; // 8% // The name of the application — never changes at runtime. static final String APP_NAME = "BudgetTracker Pro"; public static void main(String[] args) { // Using the constants in real logic int currentLoginAttempts = 3; double itemPrice = 49.99; // Calculate the tax amount using our locked-in constant double taxAmount = itemPrice * SALES_TAX_RATE; double totalPrice = itemPrice + taxAmount; System.out.println("Welcome to " + APP_NAME); System.out.println("Item price: $" + itemPrice); System.out.println("Tax (" + (SALES_TAX_RATE * 100) + "%): $" + taxAmount); System.out.println("Total: $" + totalPrice); System.out.println("Login attempts remaining: " + (MAX_LOGIN_ATTEMPTS - currentLoginAttempts)); // ❌ Try uncommenting the line below — Java will REFUSE to compile. // MAX_LOGIN_ATTEMPTS = 10; // Error: cannot assign a value to final variable } }
final on a primitive locks the value permanently. But final on an object (like an ArrayList) only locks the reference — the object itself can still be modified. So a final List can still have items added to it. This trips up even intermediate developers. If you want a truly immutable object, that's a separate discussion involving immutable classes.final reference to a mutable object does not make the object immutable.static final for constants reduces memory footprint — one per class instead of per instance.final prevents reassignment — not mutation.final with static for class-level constants.Variable Initialization: Default Values, Local vs Instance, and the 'var' Keyword
One of the most common sources of confusion is when variables get their first value. Java applies different rules depending on where a variable is declared.
Local variables — those inside a method, constructor, or block — must be explicitly assigned a value before they are used. The compiler will refuse to compile code that reads from an uninitialised local variable. This is a feature, not a bug: it prevents a whole class of bugs that plague languages like C or JavaScript.
Instance variables (fields) and static variables get default values automatically if you don't set them. Numbers default to 0 (0.0 for floating point), booleans default to false, and any object reference defaults to null. These defaults are often the source of subtle bugs — especially the null default for object references, which leads to NullPointerException at runtime.
Java 10 introduced the var keyword for local variables, allowing type inference. var tells the compiler 'figure out the type from the initialiser.' var count = 10; is equivalent to int count = 10;. But var is not a magic 'any type' — the type is still fixed at compile time, and you cannot reassign a different type. var is only allowed for local variables with an initializer, and using it incorrectly can make code harder to read.
public class InitializationAndVar { // Instance variable — gets default value 0.0 double balance; // Static variable — gets default value null for String static String defaultName; public static void main(String[] args) { // Local variable — MUST be initialised before use int localCount; // System.out.println(localCount); // Compile error: variable might not have been initialised localCount = 10; // now it's safe System.out.println("Local count: " + localCount); // Using var for type inference — only for local variables var message = "Hello Java"; // String inferred var price = 19.99; // double inferred // price = "not a number"; // Compile error: String can't be assigned to double System.out.println("Message: " + message); System.out.println("Price: " + price); // Instance and static variables: access through object or class InitializationAndVar obj = new InitializationAndVar(); System.out.println("Default balance: " + obj.balance); // prints 0.0 System.out.println("Default name: " + defaultName); // prints null } }
var when the type is obvious from the initialiser, like var list = new ArrayList<String>(). Avoid var when the type is not clear, e.g. var result = someMethod() — it hurts readability. Also never use var for return types or method parameters; that requires var-like features from Java 10 for locals only.var for a method call that returned a Map<String, List<Customer>> — the reader couldn't tell what the variable represented without digging into the method.var only when the type is immediately visible; always initialize local variables explicitly.var only when the type is clear from context.var for brevity.Where Variables Live — Local, Instance and Static Variables Explained
Not all variables are created equal — where you declare a variable determines who can use it, how long it lives, and what its default value is. Java has three distinct categories, and mixing them up is one of the most common sources of early confusion.
Local variables live inside a method. They're born when the method is called and destroyed when it returns. They have no default value — Java demands you initialize them before use, full stop.
Instance variables (also called fields) live inside a class but outside any method. Each object you create from that class gets its own private copy of these variables. They DO get default values: 0 for numbers, false for booleans, null for objects. They live as long as the object lives.
Static variables also live at the class level, but there's only ONE copy shared across every object of that class. Change it in one place and every object sees the new value. They're perfect for things like counting how many objects have been created, or storing shared configuration.
Think of it this way: static variables are like a whiteboard in a shared office (everyone sees the same thing), instance variables are like each person's personal notebook (each person has their own copy), and local variables are like a sticky note you write during a phone call and throw away when you hang up.
public class VariableScopeDemo { // STATIC VARIABLE — one shared copy for the entire class. // Every time we create a new BankAccount, this counter goes up by 1. static int totalAccountsCreated = 0; // INSTANCE VARIABLES — each BankAccount object gets its own copies. // Java sets these to defaults: 0.0 for double, "" is NOT the default for // String — the default is null. We initialize explicitly to be safe. String accountHolderName; double accountBalance; // Constructor — called when we create a new BankAccount object VariableScopeDemo(String holderName, double initialBalance) { // LOCAL VARIABLE — only exists inside this constructor. // We use it temporarily before it disappears when the constructor ends. String welcomeMessage = "Account created for: " + holderName; // Assigning values to the instance variables this.accountHolderName = holderName; this.accountBalance = initialBalance; // Incrementing the SHARED static counter totalAccountsCreated++; System.out.println(welcomeMessage); // use the local variable } void displayBalance() { // LOCAL VARIABLE — only lives during this method call String balanceReport = accountHolderName + " | Balance: $" + accountBalance; System.out.println(balanceReport); } public static void main(String[] args) { // Creating two separate account objects VariableScopeDemo account1 = new VariableScopeDemo("James Okafor", 1500.00); VariableScopeDemo account2 = new VariableScopeDemo("Priya Nair", 3200.50); // Each object has its OWN instance variables account1.displayBalance(); account2.displayBalance(); // But there is only ONE static variable — shared by both System.out.println("Total accounts ever created: " + totalAccountsCreated); } }
The Constant Interface Is a Trap — Here Is What Happens at 2 AM
You inherit a codebase. Some senior before you decided to stuff all constants into an interface called CalculatorConstants. Classes implement it to get PI and UPPER_LIMIT without qualifying them. Looks clean in a code review. Then your team needs to change a constant value. You recompile only the interface because it's a microservice deploy. Now half your JVMs see the old value, half see the new one. The compiler inlined the constant into every referencing class at compile time. You now have a silent data corruption bug in production. That's why the constant interface pattern is considered harmful. Java's final modifier on a static field in the interface doesn't help — the compiler still inlines static final primitives and Strings. If you must share constants, use a non-instantiable utility class with a private constructor. Force recompilation of all consumers when the value changes. Your sleep schedule will thank you.
// io.thecodeforge public interface CalculatorConstants { double PI = 3.14159265359; double UPPER_LIMIT = 0x1.fffffffffffffP+1023; } // Don't do this: public class Calculator implements CalculatorConstants { public double area(double radius) { return PI * radius * radius; // Compiler inlines PI at compile time } } // Do this instead: public final class MathConstants { private MathConstants() {} public static final double PI = 3.14159265359; public static final double UPPER_LIMIT = 0x1.fffffffffffffP+1023; }
static final fields of primitive types and String. If you cannot recompile every consumer, use a method call instead: public static double pi() { return PI; }. The method call is never inlined across compilation units.Enums Beat Constants Classes for Business Logic — Here Is Why
You see a Constants class with a hundred fields: MAX_RETRY_COUNT, ORDER_STATUS_PENDING, ORDER_STATUS_SHIPPED. Every service imports it. It becomes a dumping ground. Nobody knows which constant belongs where. The compiler inlines primitives across microservices. When you need to add a new order status, you touch the Constants class. Deploy a new version of order-service without recompiling fulfillment-service — and fulfillment-service still sees the old inlined value. The fix is an enum. Enums are not inlined. Enums carry behavior. OrderStatus.PENDING has a description, a next step, a timeout value. You can add a field, a method, a default implementation. The compiler treats enum constants as object references — they cannot be inlined across compilation units. You change the enum, recompile the enum JAR, and every consumer picks up the new value on restart. No silent inconsistency. Plus, your code reads like a domain language: if (order.getStatus() == OrderStatus.SHIPPED). Stop using public static final String for business states. Use enums. Your production incidents will drop.
// io.thecodeforge public enum OrderStatus { PENDING("PENDING", "Order placed, awaiting payment"), SHIPPED("SHIPPED", "Order dispatched to carrier"), DELIVERED("DELIVERED", "Order confirmed delivered"); private final String code; private final String description; OrderStatus(String code, String description) { this.code = code; this.description = description; } public String getCode() { return code; } public String getDescription() { return description; } public boolean canCancel() { return this == PENDING; } } // Usage — zero chance of inlining inconsistency: public class OrderService { public void processOrder(Order order) { if (order.getStatus() == OrderStatus.PENDING) { // Type-safe. Never a silent mismatch. } } }
static final String ORDER_STATUS_SHIPPED = "SHIPPED" in a Constants class gets inlined. Change it in the JAR, but don't recompile the consumer — the consumer still sees the old string. An enum never gets inlined. Use enums for any value that represents a fixed set of business states.The Shared Counter That Crashed a Trading Platform
int is atomic. In Java, count++ is three separate operations: read, add, write — not thread-safe.int tradeCounter = 0 was incremented via tradeCounter++ inside a method called by multiple threads. No synchronisation or atomic class used.int with AtomicInteger and used incrementAndGet(). Alternatively, synchronised the increment method, but AtomicInteger is faster for simple counters.- Never assume single-line operations are atomic in Java.
- Use
AtomicIntegerorsynchronizedfor shared mutable state. - For constants that must be thread-safe, prefer
finalimmutable values.
Add System.out.println("Variable: " + myVar) before the crashUse IDE breakpoint and evaluate expressionSystem.out.println("x = " + x + ", y = " + y)Check the scope of variables — are they shadowed?Use `jstack <pid>` to capture thread dumpSearch for the variable name in the stack traces| Aspect | Variable | Constant (final) |
|---|---|---|
| Can value change after assignment? | Yes — reassign any time | No — compile error if you try |
| Keyword used | None needed (just the type) | final keyword before the type |
| Naming convention | camelCase (e.g. playerScore) | SCREAMING_SNAKE_CASE (e.g. MAX_SCORE) |
| Typical use case | Data that changes: scores, names, counts | Fixed data: tax rates, limits, app config |
| Default value (instance/static) | 0, false, or null depending on type | Same — but must be assigned exactly once |
| Enforced by compiler? | N/A — values flow freely | Yes — compiler blocks any reassignment attempt |
| Common location | Inside methods or as class fields | Class level, usually with static final |
| File | Command / Code | Purpose |
|---|---|---|
| VariableBasics.java | public class VariableBasics { | What a Variable Actually Is |
| NamingConventions.java | public class NamingConventions { | Variable Naming Rules and the Conventions That Get You Hired |
| ConstantsDemo.java | public class ConstantsDemo { | Constants With `final` |
| InitializationAndVar.java | public class InitializationAndVar { | Variable Initialization |
| VariableScopeDemo.java | public class VariableScopeDemo { | Where Variables Live |
| ConstantAntiPattern.java | public interface CalculatorConstants { | The Constant Interface Is a Trap |
| OrderStatusEnum.java | public enum OrderStatus { | Enums Beat Constants Classes for Business Logic |
Key takeaways
final keyword makes a variable a constantstatic at the class level for true shared constants.numberOfActiveUsers not n) is what separates code that gets approved in review from code that gets sent back.Common mistakes to avoid
3 patternsUsing a local variable before initializing it
int totalScore;) but forget to assign it a value before using it in a print or calculation.int totalScore = 0;.Confusing `=` (assignment) with `==` (comparison)
if (playerScore = 100) instead of if (playerScore == 100) is a logic error. In Java, single = assigns a value; double == compares two values. Java will actually catch this specific case and throw a compile error since the result of an assignment isn't a boolean, but understanding the distinction early prevents subtle bugs in languages where this compiles silently.== for equality checks, and = only for assignment. For boolean comparisons, consider using if (condition) directly.Trying to reassign a `final` variable
final int MAX_RETRIES = 3; and then later writing MAX_RETRIES = 5; causes a compile error: 'cannot assign a value to final variable MAX_RETRIES'.final if the value needs to change, or rethink your design — maybe you actually want a regular variable. The error is a feature: it means Java caught your mistake before it became a runtime bug.Interview Questions on This Topic
What is the difference between a variable and a constant in Java, and how do you declare each one?
final keyword. Variables use camelCase naming; constants use SCREAMING_SNAKE_CASE. Example: int age = 30; is a variable; final int MAX_AGE = 120; is a constant. Constants are often declared static final at the class level.What are the three types of variables in Java (local, instance, static), how do they differ in terms of scope and lifetime, and which ones get default values?
If you declare a `final` reference variable pointing to an ArrayList, can you still add items to that list? Why or why not — and what does this tell you about what `final` actually guarantees?
final on a reference variable only prevents reassignment of the reference itself—it does not make the object immutable. The reference always points to the same ArrayList object, but that object's internal state can change. This means final guarantees that the variable always refers to the same object, not that the object is constant. To make the list unmodifiable, you'd need Collections.unmodifiableList() or use an immutable collection from Java 9+.Frequently Asked Questions
int stores whole numbers only (like 5, -200, 1000) and uses 32 bits of memory. double stores numbers with a decimal point (like 3.14, -0.5, 99.99) and uses 64 bits. Use int for counts and indexes, and double for measurements, prices, or anything that needs a fractional part.
No — this is a hard rule enforced by the compiler. Variable names must start with a letter, an underscore _, or a dollar sign $. Starting with a number (like 2ndPlace) will give you a compile error immediately. In practice, just start every variable name with a lowercase letter and you'll never hit this issue.
Java assigns it a safe default value automatically. Numbers default to 0 (or 0.0 for double/float), booleans default to false, and any object reference defaults to null. This only applies to instance and static variables — local variables inside methods have no default and must be explicitly initialized before you use them.
No. var is only allowed for local variable declarations with an initializer. It cannot be used for method parameters, return types, fields, or catch parameters. The type is inferred at compile time and remains fixed.
20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.
That's Java Basics. Mark it forged?
6 min read · try the examples if you haven't