Operating Systems Lecture Notes

27 February 2012 • Pop Quiz 2


A Question

Static Fields

Static Field References

Add Threads for Fun

The Answer

Postscript

From: rclayton@monmouth.edu (R. Clayton)
To: s0------@monmouth.edu
Subject: Pop quiz 2 answer.
This is what the FindBugs site definition says, “ST: Write to static field from instance method (ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD) This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.”, a close match of what my answers was on the question.

First, there’s no arguing pop-quiz grades.

Second, the findbugs explanation doesn’t make sense. To what does “multiple instances” refer? Not the static field, because there’s only one instance of a static field. Not to the class instances because, while there are several of them, they are manipulating the static field; they are not being manipulated themselves. What else is there to refer to?

Third, assuming we can assign some kind of meaning to the findbugs explanation, why is it “tricky to get correct?” (And what does it mean to be “correct,” which turns out to be a hard question, one that Java gets mostly wrong). Why is it considered bad practice? Used in the way (we imagine) findbugs describes, a static field is effectively a limited-scope global variable, and that’s usually considered both bad style and bad practice, but so what? There’s an answer more directly relevant to the class, which is why I gave the verbal clue about covering the answer to the pop quiz in the lectures, and why I gave the pop quiz at all.


This page last modified on 2012 February 27.

Creative
    Commons License