Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Java - Static Keyword
Identify the problem in this code:
class Example {
    static int number;
    void setNumber(int number) {
        this.number = number;
    }
}
ACannot use 'this' to refer to static variable
BStatic variables cannot be assigned values
CMethod setNumber must be static
DNo problem, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Understand 'this' keyword usage

    'this' refers to the current object instance and cannot be used for static variables.
  2. Step 2: Check assignment to static variable

    Static variable 'number' should be accessed by class name or directly, not with 'this'.
  3. Final Answer:

    Cannot use 'this' to refer to static variable -> Option A
  4. Quick Check:

    'this' with static variable error = B [OK]
Quick Trick: 'this' is for instance, not static members [OK]
Common Mistakes:
  • Using 'this' to access static variables
  • Thinking static variables can't be assigned
  • Believing non-static method must be static here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes