How do you declare JSP variables as "final"?

P

phillip.s.powell

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
 
M

Mark Space

<%

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....
 
A

Andrea Francia

<%

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);

%>
 
P

phillip.s.powell

Please see below for comments

<%

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!
 
L

Lew

<%

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.
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top