🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8080

Re: Local Variable Needs to be Declared Final

#8080
From: Torsten Welches
Date: Tue, 24 Apr 2001 06:37
75 lines
3603 bytes
Harry,

the syntax is quite simple, just use the final keyword:

final PrinterJob printerJob = PrinterJob.getPrinterJob();

The reason is also quite simple once you fully understand what 
actually happens: 
Let me start by saying that this is not (only) about Threads 
but about inner classes. Your printthread is an inner class of 
your PrintButton_actionPerformed method. 
Now an instance of an inner class may well outlive your method
invocation, your Thread being a good example for that - it will 
probably continue to run after your PrintButton_actionPerformed 
method has long returned. 
Normally a local variable (stack) will be automatically destroyed after the method returns, because there is no way to 
access it anyway. As soon as local inner classes enter the 
picture this is no longer true. You Thread will probably use the
printerJob instance sometimes after the method has already 
returned, so the normal local-variable-cleanup would be disastrous. Marking the variable final is the way to tell the 
compiler that he has to preserve this very local variable 
even after the method has returned (on the heap).

Make sense?

Torsten

"Harry Whitehouse" <harry@envmgr.com> wrote:
>Well, I guess it's time to learn about the modifier "final"!
>
>I'm trying to spawn a thread for the printer dialog on my applet, so the
>dialog has time to close on slower connections without blanking the
>underlying applet GUI.  Heaven knows if my approach is even correct (that's
>another question <g>) but for now I need to understand about the compiler
>error message indicated a local variable (in this case printerJob) needs to
>be declared final.  I've seen this in some other threads I've created (to
>handle URL transactions), but have managed to sidestep this issue by
>declaring and using non-local variables.
>
>First, what is the proper syntax for this?  I've tried some obvious
>permutations but with no luck.
>
>And second, what is the significance of this modifier?
>
>
> void PrintButton_actionPerformed(ActionEvent e) {
>
>        PrinterJob printerJob=null;
>        bprintpostage=jCheckPrintPostage.isSelected();
>        printerJob = PrinterJob.getPrinterJob();
>
>
>       Thread printthread = new Thread() {
>           public void run() {
>              doPrint = printerJob.printDialog();
>          }
>        };
>        printthread.start();
>
>        try {
>            printthread.join();
>        }
>        catch (Exception jprintexp) {
>            jprintexp.printStackTrace(System.out);
>            return;
>        }
>
>
>TIA
>
>Harry
>
>
>
Technical Details
Message-ID: <3ae51f20$1_1@dnews>
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!sn-xit-04!supernews.com!news.cs.utwente.nl!newsfeed.wirehub.nl!news.tele.dk!212.74.64.35!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3ae50e3c$1_1@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,4be1a32d6ef1248
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-04-23 23:56:04 PST
From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Subject: Re: Local Variable Needs to be Declared Final
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 193.158.55.74 193.158.55.74    
References:  <3ae50e3c$1_1@dnews> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3ae51f20$1_1@dnews>
Date: 23 Apr 2001 23:37:20 -0700
X-Trace: dnews 988094240 207.105.83.65 (23 Apr 2001 23:37:20 -0700)
Lines: 76
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!sn-xit-04!supernews.com!news.cs.utwente.nl!newsfeed.wirehub.nl!news.tele.dk!212.74.64.35!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews