Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do you declare JSP variables as "final"?

Reply
Thread Tools

How do you declare JSP variables as "final"?

 
 
phillip.s.powell@gmail.com
Guest
Posts: n/a
 
      02-29-2008
Consider this code:


<%

MailAdminReader mar = new MailAdminReader();
boolean hasMail = false;

Thread t = new Thread(new Runnable() {
public void run() {
try {
hasMail = mar.checkForMail();
} catch (Exception e) {
// NO RESPONSE - hasMail REMAINS FALSE
}
}
});

t.start();
t.join(5000);

%>



This section of code should throw a compilation error "variables must
be declared final" on "mar" and "hasMail".

But here's the problem: they're in a JSP. How in the world do I
declare these variables "final" when there is no class reference that
can be declared final? DO I use a block or can I? I am lost here.

Thanks
Phil
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      02-29-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> <%
>
> MailAdminReader mar = new MailAdminReader();
> boolean hasMail = false;


> But here's the problem: they're in a JSP. How in the world do I
> declare these variables "final" when there is no class reference that
> can be declared final? DO I use a block or can I? I am lost here.


I haven't sussed this all out, but a few things occur to me.

First, final is legal for local variable declarations. Can you just add
final before "MailAdminReader mar = ..."?

Second, there's an obvious place to add a final variable to the
anonymous class itself.

Thread t = new Thread(new Runnable() {
final MailAdminReader mar2; // = etc. <--- new line
public void run() {
try {
hasMail = mar2.checkForMail(); // <- changed
} catch (Exception e) {
// etc.


Third you can always declare a method in your JSP for the purpose of
making the parameter final:

<%!
private void someMethod( final MailAdminReader mar2 ) {
Thread t = new Thread( new Runnable() {
// etc. use mar2 not mar...
}
}
%>

<% someMethod( mar ); %>


Sorry I don't work with anonymous classes enough to get exactly the
right one for you, but that should give you some ideas anyway....
 
Reply With Quote
 
 
 
 
Andrea Francia
Guest
Posts: n/a
 
      02-29-2008
<%

final MailAdminReader mar = new MailAdminReader();

Thread t = new Thread(new Runnable() {
public void run() {
boolean hasMail = false;
try {
hasMail = mar.checkForMail();
} catch (Exception e) {
// NO RESPONSE - hasMail REMAINS FALSE
}
}
});

t.start();
t.join(5000);

%>
 
Reply With Quote
 
phillip.s.powell@gmail.com
Guest
Posts: n/a
 
      03-03-2008
Please see below for comments

On Feb 29, 4:17*pm, Andrea Francia <(E-Mail Removed)>
wrote:
> <%
>
> final MailAdminReader mar = new MailAdminReader();
>
> Thread t = new Thread(new Runnable() {
> * * public void run() {
> * * * *boolean hasMail = false; // SORRY BUT hasMail IS DECLARE OUTSIDE OF THREAD
> * * * *try {
> * * * * * hasMail = mar.checkForMail();
> * * * *} catch (Exception e) {
> * * * * * // NO RESPONSE - hasMail REMAINS FALSE
> * * * }
> * * }
>
> });
>
> t.start();
> t.join(5000);
>
> %>



Boolean hasMail has to be declared outside of the thread because it's
used there. How on earth do you make hasMail final within JSP? That's
got to be impossible to do!
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-03-2008
(E-Mail Removed) wrote:
> <%
>
> MailAdminReader mar = new MailAdminReader();
> boolean hasMail = false;
>
> Thread t = new Thread(new Runnable() {
> public void run() {
> try {
> hasMail = mar.checkForMail();
> } catch (Exception e) {
> // NO RESPONSE - hasMail REMAINS FALSE
> }
> }
> });
>
> t.start();
> t.join(5000);
>
> %>


> Boolean hasMail has to be declared outside of the thread because it's
> used there. How on earth do you make hasMail final within JSP? That's
> got to be impossible to do!


It's actually quite possible, except that you want to change the value of the
boolean.

Remember, scriptlet (which you should avoid - separate conversation) is just
code inserted into the service() method of the resulting servlet.

I will assume that the variable 'mar' is used after the thread join(),
otherwise you could declare it inside the Runnable run() method.

Simply add the 'final' decoration to your variable declaration:
final MailAdminReader mar = new MailAdminReader();

Unfortunately, since you want to change the value of the boolean, that won't
work for the boolean. It will work for a spontaneous holder class, though:

<%
final MailAdminReader mar = new MailAdminReader();
class Result
{
public boolean hasMail; // initialized automatically
}
final Result result = new Result();
Thread t = new Thread( new Runnable ()
{
public void run()
{
try
{
result.hasMail = mar.checkForMail();
}
catch ( Exception ignore )
{}
}
}
);
// etc.
%>

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-03-2008
Lew wrote:
> <%
> final MailAdminReader mar = new MailAdminReader();
> class Result
> {

// oops! forgot to say
volatile
> public boolean hasMail; // initialized automatically
> }
> final Result result = new Result();
> Thread t = new Thread( new Runnable ()
> {
> public void run()
> {
> try
> {
> result.hasMail = mar.checkForMail();
> }
> catch ( Exception ignore )
> {}
> }
> }
> );
>
> t.start();
> t.join(5000);
> // etc.
> %>


--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
Best Way to Define/Declare/Initialize Variables Simultaneously? David T. Ashley C++ 6 12-05-2004 09:37 AM
declare vs define of global variables Brian Sammon C Programming 7 10-08-2004 02:39 PM
why don't you have to declare variables in javascript? Andy Fish Javascript 10 08-21-2003 08:42 AM



Advertisments