Given:
and
What is the result?
Given:
and
What is the result?
The 'city' variable is declared as static in the 'Location' class. Static variables are shared among all instances of the class and are not associated with any particular instance. Therefore, setting 'location' to null does not affect the 'city' variable. When the 'Location' object is created, the constructor sets the static 'city' variable to 'San Francisco'. The garbage collection and the nullification of 'location' do not affect the static variable. Hence, when 'Location.city' is printed, it outputs 'San Francisco'.
Static variables/ fields are not garbage collected. Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.
is valid, static field city
Tested: C. San Francisco.
C is correct TESTED