How do you declare JSP variables as "final"?

Discussion in 'Java' started by phillip.s.powell@gmail.com, Feb 29, 2008.

  1. Guest

    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
    , Feb 29, 2008
    #1
    1. Advertising

  2. Mark Space Guest

    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....
    Mark Space, Feb 29, 2008
    #2
    1. Advertising

  3. <%

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

    %>
    Andrea Francia, Feb 29, 2008
    #3
  4. Guest

    Please see below for comments

    On Feb 29, 4:17 pm, Andrea Francia <>
    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!
    , Mar 3, 2008
    #4
  5. Lew Guest

    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
    Lew, Mar 3, 2008
    #5
  6. Lew Guest

    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
    Lew, Mar 3, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,732
  2. David T. Ashley
    Replies:
    6
    Views:
    537
    Tim Rentsch
    Dec 5, 2004
  3. Replies:
    0
    Views:
    4,347
  4. Replies:
    5
    Views:
    510
    Chris Uppal
    Nov 17, 2006
  5. Andy Fish
    Replies:
    10
    Views:
    176
    Andy Fish
    Aug 21, 2003
Loading...

Share This Page