QuestionQ151

Working with Methods and Encapsulation

Given the following code:

Question Image

What is the outcome?

  • A 10
  • B A compile time error occurs
  • C 20
  • D 10
Explanation

A static method belongs to the class rather than to a particular object, so it cannot directly access the instance field foo. The statement foo += 10; therefore causes a compile-time error.

Community Discussion

No comments yet. Be the first to start the discussion!