aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Fields Initailization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Fields Initailization" Watch "Fields Initailization" New topic
Author

Fields Initailization

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Hello all,
I was going thru the JLS, 8.3.2.3(Restrictions on the use of Fields during Initialization).
I am not able to understand the head and tail of that explanation.
Well, i have the doubt regarding that, please explain me in simple words:

class Test {
float f = j;
static int j = i;
}

// The above piece of code doesnt generate any compilation errors

While the code below gives compilation error:
class Z {
static int i = j +2;
static int j = 4;
}
WHY???
I know the explanation is given in the JLS, but i am not able to understand it, please someone make me understand in simple words, it seems really obscure to me
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
I've not read the JLS, but here's my guess. If anyone thinks I'm wrong let me know.
First of all, I think you didn't mean to write a i in the code, right?
I'll consider that being a 1.
A static field belongs to a class and not to any instance. That's way you can even use a static member without any instance of that class. The static fields are constructed by the jvm before any instance of that class exists.
When you write this:


The initialization of f or anyother instance variable is probably made after the initialization of the static members (because the reasons I mentioned earlier). That's why it compiles. When the compiler initializes f it has already initialized j.
But when you write this:

Now, the compiler checks that i is a static member and tries to initialize, but in this case, member j has not been initialized yet.
Got the point? Both of them are static.
I don't know if it's correct, but we could say that the compiler has defined 2 steps (among other steps that doesn't matter right now).
1 - Definition of static members.
2 - Definition of instance members.
Well, hope I'm correct and you have understood.
[ February 07, 2005: Message edited by: Leandro Melo ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
in the second case you reference a static classmember before it has been initialised, causing an error.
In the first, you reference a static classmember from a nonstatic member. As all static members are initialised and resolved before any class instance is ever created this works.

Of course if you were to compile your first testcase as you quote it here compilation will fail because there's no static (or indeed any) field called 'i', let alone one that can be cast to int.


42
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


sorry but I am not getting , why compiler giving error as compiler doesn't vary about static member .

thanks .
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I thought Jeroen explained it quite well.

This also doesn't compile, complains about a forward reference:


The exceptional situation is when one is static the other isn't. In that case it only LOOKS like a forward reference, but it isn't. Because statics are initialized before instance variables, right?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The principal that applies here, described in Mughal as the declare-before-read rule, is that in initializer expressions a variable can be on the left side of = before it is declared but it cannot be on the right side of = until it is declared.


Mike Gershman
SCJP 1.4, SCWCD in process
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Mike, i get compiler errors in your code (I'm using jdk 1.4.2).

[ February 08, 2005: Message edited by: Leandro Melo ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
My exact cut and pasted code:


I used javac 1.5.0 and only got an expected error on "i = j;".

What message did you get for "j = i;"?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I got the same result with javac 1.4.1_01 as I did with javac 1.5.1
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
I get "j cannot be resolved" for both errors.
This is weird if you say you're not getting the error.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Huummm, this seems interesting.
I got it now, but anyway I'd like an explanation if you could...
In the following code, i get only one error (as you mentioned).


But here's the code I was getting the 2 errors.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


Can any body explain me this ...

thanks a lot .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Leandro, if you put a block inside a method, it is not an initialization block and the assignment expressions are not initializers. The rule is define by first use.

Rathi, if you scroll up you will see my explanation.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Thanks Mike.
Anyway, would you know which section of jls handles the details you just mentioned? I'd like to take a careful look.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
would you know which section of jls handles the details you just mentioned? I'd like to take a careful look.

Check out JLS section 8.3.2.3 and the sections just above it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fields Initailization