Article View: borland.public.jbuilder.java.language
Article #8088Re: Local Variable Needs to be Declared Final
From: Harry Whitehouse
Date: Tue, 24 Apr 2001 19:54
Date: Tue, 24 Apr 2001 19:54
85 lines
3855 bytes
3855 bytes
Beautifully explained! My thanks once again!
Harry
"Torsten Welches" <torsten.welches@who.cares.netcologne.de> wrote in message
news:3ae51f20$1_1@dnews...
>
> 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:
<3ae5da04_1@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!newspeer.monmouth.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ae50e3c$1_1@dnews> <3ae51f20$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-24 13:14:03 PST From: "Harry Whitehouse" <harry@envmgr.com> Newsgroups: borland.public.jbuilder.java.language References: <3ae50e3c$1_1@dnews> <3ae51f20$1_1@dnews> Subject: Re: Local Variable Needs to be Declared Final Date: Tue, 24 Apr 2001 12:54:50 -0700 Lines: 87 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 38.168.115.135 Message-ID: <3ae5da04_1@dnews> X-Trace: dnews 988142084 38.168.115.135 (24 Apr 2001 12:54:44 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!newspeer.monmouth.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews