Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

How do you declare JSP variables as "final"?

Thread view: 
phillip.s.powell@gmail.com - 29 Feb 2008 20:21 GMT
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
Mark Space - 29 Feb 2008 21:16 GMT
> <%
>
> 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....
Andrea Francia - 29 Feb 2008 21:17 GMT
<%

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

%>
phillip.s.powell@gmail.com - 03 Mar 2008 14:43 GMT
Please see below for comments

On Feb 29, 4:17 pm, Andrea Francia <andrea.fran...@gmx.it.invalid>
wrote:
> <%
>
[quoted text clipped - 16 lines]
>
> %>

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!
Lew - 03 Mar 2008 15:43 GMT
> <%
>
[quoted text clipped - 15 lines]
>
> %>

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

Signature

Lew

Lew - 03 Mar 2008 16:12 GMT
> <%
>   final MailAdminReader mar = new MailAdminReader();
>   class Result
>   {
// oops! forgot to say
      volatile
>     public boolean hasMail; // initialized automatically
>   }
[quoted text clipped - 17 lines]
> // etc.
>  %>

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.