Static Variables in Java: Class vs Instance Variables - Quiz
Total: 7 questions
1. What is a static variable in Java?
What is a static variable in Java?
A static variable (also called a static field or class variable) is a field declared inside a class with the static keyword, for example static int b;. It belongs to the class itself, not to any object: there is exactly one copy for the whole class, it is created once when the class is loaded and initialized, and it can be accessed without creating an object.
If not explicitly initialized, a static field gets a default value: 0 for numeric types, false for boolean, null for reference types. It is not a true global variable: it always belongs to a specific class, and its visibility is controlled by access modifiers.
2. What is the difference between a class (static) variable and an instance variable in Java?
What is the difference between a class (static) variable and an instance variable in Java?
An instance variable (int a;) exists separately in every object, is created with new, and is accessed through an object: obj.a. From a static method it can only be reached through an object reference. It holds the state of a specific object (color, name).
A static variable (static int b;) has one copy per class, is created once when the class is loaded and initialized, and is accessed through the class name: Counter.b, or directly from a static method. Typical uses are counters, constants, and shared settings. Changing a static field through one object makes the new value visible through all others.
3. How should you access a static variable, and why not through an object?
How should you access a static variable, and why not through an object?
From another class, access a static variable through the class name: Counter.b. Inside the same class you can simply write b.
Access through an object (counter1.b) also compiles, but it is discouraged: it misleads readers into thinking the field is per-object, and most IDEs flag it with a warning. The compiler resolves such access through the declared type anyway, so even Counter c = null; c.b does not throw NullPointerException.
4. How can you count created objects of a class with a static variable, and what is the problem in multi-threaded code?
How can you count created objects of a class with a static variable, and what is the problem in multi-threaded code?
Declare private static int count = 0; in the class and increment it in the constructor (count++), since the constructor runs every time an object is created. Expose the value through a static method getCount(). Each Ball has its own color, but count is shared, so after two new Ball(...) calls Ball.getCount() returns 2.
The problem: count++ is not atomic, and a static field is visible from every thread. When objects are created concurrently from several threads, the counter can silently lose increments. In multi-threaded code, use AtomicInteger or proper synchronization.
5. How do you declare a constant in Java, and does Java have a const keyword?
How do you declare a constant in Java, and does Java have a const keyword?
A constant combines static (one value per class) and final (the value cannot change after it is assigned). By convention its name is written in uppercase with underscores: public static final int MAX_USERS = 100;. Assigning a new value is a compile error.
If a constant of a primitive type or String is initialized with an expression known at compile time, it is a compile-time constant: the compiler inlines its value at every use, and it can be used as a case label in a switch. const is a reserved word in Java, but it is not used — you cannot declare a variable with it.
6. What is the difference between static and final, and does a final reference make the object immutable?
What is the difference between static and final, and does a final reference make the object immutable?
static decides who owns the variable: the class (a single shared copy) or each object (its own copy). final decides whether it can be reassigned after the initial value is set. The two are independent: a static variable without final (a counter), a final field without static (an immutable per-object id), or both together — a constant.
final on a reference variable only prevents reassigning the reference, not changing the object it points to: for static final List<String> NAMES = new ArrayList<>(); calling NAMES.add(...) is allowed. For a truly immutable collection, use List.of(...).
7. When is a static variable initialized, and where is it stored in memory?
When is a static variable initialized, and where is it stored in memory?
A static variable is initialized once, during class initialization — before the first object is created, the first static method is called, or the first static field is accessed (compile-time constants are the exception). Static field initializers and static blocks run in the order they appear in the class.
In HotSpot JVMs since JDK 7, the values of static fields live on the heap, attached to the java.lang.Class object of their class; before that they were in PermGen. Java 8 replaced PermGen with Metaspace, which stores class metadata but not the values of static fields.