a question related to "final static" field variable

S

Stefan Ram

Knute Johnson said:
Is there any practical difference between;
static final String s1 = "A";
and
static final String s1;
static { s1 = "A" };

If you deem it a »practical difference«
that the second text will not compile.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Stefan said:
If you deem it a »practical difference«
that the second text will not compile.

It compiles with the missing semicolon added.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Is there any practical difference between;

static final String s1 = "A";

and

static final String s1;
static { s1 = "A" };

I do not think so.

The reason to use the "static constructor" (if you will forgive
me for using the C# term) is if you need multiple statements to
calculate the value.

Arne


Arne
 
T

Tom Hawtin

Knute said:
Patricia said:
public static void main(String[] args) {
rootDir = args[0];
Access to rootDir needs synchronization if it is going to be accessed by
multiple threads which is guaranteed to be the case here.

Counterpoint of pedantry: Except for obscure things such as finalisers
and assuming the initialisation of the class does not start any threads
and the class is being started as a normal program, then there is a
happens-before relation between any started threads and the single
assignment.

OTOH, I would avoid statics on the principle of no broken windows. Or I
think in the case of statics it can be termed the principle of "no
crapping on the carpet".

Tom Hawtin
 
T

Tom Hawtin

Arne said:
I do not think so.

Yup, javap -c should show identical byte code.
The reason to use the "static constructor" (if you will forgive
me for using the C# term)

Nooooo!!1! "static initialiser" (although technically initialiser should
be missplled)
is if you need multiple statements to
calculate the value.

Some people prefer defining a function to do that. I prefer the static
initialiser. Note, you can't qualify with the class in the assignment
("FinalDemo.s1 = "A";"). However, never use that as an excuse to miss
the final!

Tom Hawtin
 
K

Knute Johnson

Tom said:
Knute said:
Patricia said:
public static void main(String[] args) {
rootDir = args[0];
Access to rootDir needs synchronization if it is going to be accessed
by multiple threads which is guaranteed to be the case here.

Counterpoint of pedantry: Except for obscure things such as finalisers
and assuming the initialisation of the class does not start any threads
and the class is being started as a normal program, then there is a
happens-before relation between any started threads and the single
assignment.

Isn't that only true if assignment occurs in the constructor? We don't
have a whole program here but I thought you could not ensure that
assignment 'happens before' any other access to this variable given the
possible instruction reordering. On the other hand after the
constructor is complete it should be safe assuming it is not assigned again.
OTOH, I would avoid statics on the principle of no broken windows. Or I
think in the case of statics it can be termed the principle of "no
crapping on the carpet".

There is no doubt that many will shudder at the thought :).
 
T

Tom Hawtin

Knute said:
Isn't that only true if assignment occurs in the constructor? We don't
have a whole program here but I thought you could not ensure that
assignment 'happens before' any other access to this variable given the
possible instruction reordering. On the other hand after the
constructor is complete it should be safe assuming it is not assigned
again.

The point is that the assignment is more or less the first thing the
program does. Any threads are created afterwards. It's the starting of
threads that gives the happens-before, not the static initialisation.

(For (instance) constructors, the variable would need to be final and
the instance should not leak before the end of the constructor.)

Tom Hawtin
 

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,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top