java.awt.Graphics: not a candidate for AutoCloseable?

Phil Race philip.race at
Tue Feb 22 20:19:34 UTC 2011

Aside from the fact that its weird, if not downright wrong
to think of a Graphics instance as "closeable",
Graphics instances are not used in the same way as all
the stream type resources for which AutoCloseable is intended.
In most cases they are provided  to you in a call to paint()
Releasing a Graphics via explicit call to dispose() only
should be used in the relatively rare use case where you obtained it
yourself to draw on an image or some other surface.
And even then dispose() is not a requirement since the system does 
dispose of
the graphics for you in a prompt way that does not require finalisation.
The docs on java.awt.Graphics are extremely dated in that respect.


On 2/22/2011 12:00 PM, assembling signals wrote:
> Hello, community!
> Is java.awt.Graphics not a candidate for AutoCloseable?
> In most cases, a Graphics object has to be dispose()'d at the end.
> Couldn't it be extended with close() which would be a delegate to dispose()?
> Best regards,
> Ivan G Shevchenko

More information about the core-libs-dev mailing list