diff --git a/src/java.logging/share/classes/java/util/logging/StreamHandler.java b/src/java.logging/share/classes/java/util/logging/StreamHandler.java
index 320638d91a158..217587dfd7a4a 100644
--- a/src/java.logging/share/classes/java/util/logging/StreamHandler.java
+++ b/src/java.logging/share/classes/java/util/logging/StreamHandler.java
@@ -231,6 +231,7 @@ public void publish(LogRecord record) {
             }
         }
     }
+
     private void publish0(LogRecord record) {
         if (!isLoggable(record)) {
             return;
@@ -246,6 +247,7 @@ private void publish0(LogRecord record) {
         }
 
         try {
+            Writer writer = this.writer;
             if (!doneHeader) {
                 writer.write(getFormatter().getHead(this));
                 doneHeader = true;
@@ -295,7 +297,9 @@ public void flush() {
             }
         }
     }
+
     private void flush0() {
+        Writer writer = this.writer;
         if (writer != null) {
             try {
                 writer.flush();
@@ -309,6 +313,7 @@ private void flush0() {
 
     private void flushAndClose() throws SecurityException {
         checkPermission();
+        Writer writer = this.writer;
         if (writer != null) {
             try {
                 if (!doneHeader) {
@@ -323,8 +328,8 @@ private void flushAndClose() throws SecurityException {
                 // report the exception to any registered ErrorManager.
                 reportError(null, ex, ErrorManager.CLOSE_FAILURE);
             }
-            writer = null;
             output = null;
+            this.writer = null;
         }
     }