non static compile time constant .vs. static in tomcat

P

ptg_abhishek

I just ran findbugs and it came up with this warning - "This class
contains an instance final field that is initialized to a compile-time
static value. Consider making the field static".

What is the difference whether a set of classes use variables
initialized to a compile-time constant [string literals], or whether a
set of static variables is used [apart from the knowledge that the
static variables persist as long as the application is alive]? Could
the use of static variables in a web-application pose any problems when
handling concurrent requests [threading handled by the Server]?

regards.
-abhishek.
 
C

Chris Uppal

I just ran findbugs and it came up with this warning - "This class
contains an instance final field that is initialized to a compile-time
static value. Consider making the field static".

What is the difference whether a set of classes use variables
initialized to a compile-time constant [string literals], or whether a
set of static variables is used

None. Any final field (static or instance) that is initialised to a String
constant will (this is required by the language definition) be inlined by the
compiler at the points where the are used. The same thing applies to numeric
and boolean final fields. It also applies if the final field is not initalised
to a constant, but to a compile-time constant-valued expression.

There is little point in using an actual instance field when the value is a
fixed constant, and it takes up a little space in each object (the field is
still there, it is just not actually /used/), so you may as well use a static
constant. Makes the intent of the code clearer too (there may be exceptions to
that, but I can't think of a plausible one offhand).

-- chris
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top