Static mess

R

Razvan

Hi !




In the following code:

public class CDummy
{
static {
jj = 5;
// System.out.println("jj=" + jj);
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("jj = " + jj);
}

static int jj = 6;
}

I can assign to the primitive jj the value 5 even if the primitive is
defined later, however I cannot print its value because I use a
'forward reference' ! Can somebody explain why I can do


jj = 5;


but I cannot do

System.out.println("jj=" + jj);


I think that I should either have access to jj prior to declaration +
definition (line: static int jj = 6;) in both cases or the access
should be denied in both cases. If there is a logical explanation for
this I would like to hear it.




Regards,
Razvan
 
M

Mike Schilling

Razvan said:
Hi !




In the following code:

public class CDummy
{
static {
jj = 5;
// System.out.println("jj=" + jj);
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("jj = " + jj);
}

static int jj = 6;
}

I can assign to the primitive jj the value 5 even if the primitive is
defined later, however I cannot print its value because I use a
'forward reference' ! Can somebody explain why I can do


jj = 5;


but I cannot do

System.out.println("jj=" + jj);


I think that I should either have access to jj prior to declaration +
definition (line: static int jj = 6;) in both cases or the access
should be denied in both cases. If there is a logical explanation for
this I would like to hear it.

I don't know what you mean by "can't do it". It compiles and runs fine
(under 1.4.2).
 
I

iamfractal

Hi !




In the following code:

public class CDummy
{
static {
jj = 5;
// System.out.println("jj=" + jj);
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("jj = " + jj);
}

static int jj = 6;
}

I can assign to the primitive jj the value 5 even if the primitive is
defined later, however I cannot print its value because I use a
'forward reference' ! Can somebody explain why I can do


jj = 5;


but I cannot do

System.out.println("jj=" + jj);


I think that I should either have access to jj prior to declaration +
definition (line: static int jj = 6;) in both cases or the access
should be denied in both cases. If there is a logical explanation for
this I would like to hear it.




Regards,
Razvan

Hi, Razvan!

This is just a rule to catch circular initialization, a rule written
into the JLS itself (see
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406).

Your printing of jj is failing because it's not on the left-hand side
of an assignment. Everything trying to "see" the value of jj here will
fail, not just printing; jj++ will fail, for example.

It's failing because all static parts of the code must be initialised
atomically (though strictly there are two phases: variables are
assigned default values and then assigned values, but that's not
relevant here).

Thus, the compiler allows you to assign prior to declaration as long
as that assignment is unverified before the end of all static
sequences and declarations.

If you do try to verify in some way the value before all static
sequences are run (and they run from the top of the file down), then
you are attempting to break to atomicity of the static initialisation
phase - this is precisely what the JLS authors wanted to disallow (as
they thought, I suppose, that it would usually be an error on the part
of the designer).

Note in the JLS that this does not only apply to static
initialisation, but all initialisation of given phase: they give
examples of member variable intialisation, for example.

Of course, they could have decided to disallow even assignments prior
to declaration: they just coders too, afterall.

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
N

newton klea

Because you call it not as class variable. Try

System.out.println("jj=" + CDummy.jj);
 
T

Thomas G. Marshall

Mike Schilling coughed up:
Razvan said:
Hi !




In the following code:

public class CDummy
{
static {
jj = 5;
// System.out.println("jj=" + jj);
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("jj = " + jj);
}

static int jj = 6;
}

I can assign to the primitive jj the value 5 even if the primitive is
defined later, however I cannot print its value because I use a
'forward reference' ! Can somebody explain why I can do


jj = 5;


but I cannot do

System.out.println("jj=" + jj);


I think that I should either have access to jj prior to declaration +
definition (line: static int jj = 6;) in both cases or the access
should be denied in both cases. If there is a logical explanation for
this I would like to hear it.

I don't know what you mean by "can't do it". It compiles and runs
fine (under 1.4.2).


HUH? Are you sure? My 1.5.0 is gacking on it big time, as is consistent
with the jls, IMO anyway.

Did you remove the // in front of his erring line?
 
M

Mike Schilling

Thomas G. Marshall said:
Mike Schilling coughed up:
Razvan said:
Hi !




In the following code:

public class CDummy
{
static {
jj = 5;
// System.out.println("jj=" + jj);
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("jj = " + jj);
}

static int jj = 6;
}

I can assign to the primitive jj the value 5 even if the primitive is
defined later, however I cannot print its value because I use a
'forward reference' ! Can somebody explain why I can do


jj = 5;


but I cannot do

System.out.println("jj=" + jj);


I think that I should either have access to jj prior to declaration +
definition (line: static int jj = 6;) in both cases or the access
should be denied in both cases. If there is a logical explanation for
this I would like to hear it.

I don't know what you mean by "can't do it". It compiles and runs
fine (under 1.4.2).


HUH? Are you sure? My 1.5.0 is gacking on it big time, as is consistent
with the jls, IMO anyway.

Did you remove the // in front of his erring line?

No, I didn't realize that was his problem. That fails, of course, for the
reasons iamfractal explains.
 

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
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top