Page 1 of 1
[HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Fri Jan 04, 2019 4:29 am
by flex567
I dont think I understand clearly the following rule:
However, a static block can only set the value of a variable that is defined afterwards.
In the code "a" is set after is defined and not before.
Code: Select all
public class Temp{
static int a;
static{
System.out.println(a); //valid, a is defined before the static block
//System.out.println(b); //INVALID, b is defined after the static block
b = 10; //valid because b is being assigned a value
a = 111; // value set after it has been defined and not before ???
m(); //valid even though m is defined later
}
static void m(){
System.out.println(b); //valid, a method can do anything with a variable that is declared later in the code
}
static int b;
public static void main(String[] args){
}
//another static block
static{
System.out.println(b);
}
}
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Fri Jan 04, 2019 5:35 am
by admin
You are reading it out of context. The complete para is
A static block can access all other static members of the class, i.e., static variables and static methods even if they appear after the static blocks. However, a static block can only set the value of a variable that is defined afterwards.
access includes two things - reading a value and setting a value. So, what paragraph means is a static block cannot read a static variable that is defined afterwards. If a static variable is defined afterwards, then that variable can only be set and not read.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Fri Jan 04, 2019 6:22 pm
by flex567
aha I see.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Mon Feb 11, 2019 7:21 pm
by OCAJO1
Read the above explanation, and tested placing static int b; before, inside and after the block. Looking at the various compiler complaints, it looks like as long as static int b; is not within the block that b is being set, the compiler is happy.
Unless my testing was not to the point, before or afterwards didn't matter to the compiler. So shouldn't the sentence be something like - "However, a static block can only set the value of a variable that is defined outside of that block."
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Mon Feb 11, 2019 11:42 pm
by admin
Check out section 8.3.3 "Forward References During Field Initialization" of JLS. It will give you the exact details that you are looking for. (Not important for the exam though).
For the purpose of the exam and for most practical purposes, the information given in the book is sufficient.
Paul.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Tue Feb 12, 2019 1:23 pm
by OCAJO1
Right, in section 8.3.3 as well, if the variable count in declared inside the block where that variable is being used (in this case incremented or decremented) the complier doesn't like it. But if it is declared before or after that block (even if in some other block) the code compiles. So I still think in the sentence "However, a static block can only set the value of a variable that is defined afterwards.", the word afterwards only reflects part of what the compiler allows.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Tue Feb 12, 2019 9:39 pm
by admin
The following doesn't compile:
Code: Select all
class TestClass{
public class TestClass {
static{
System.out.println(b); //invalid, b is being read here but is defined aftwards
b = 10; //valid, b is being set here
}
static int b;
}
This is exactly what the statement in the book says. So I am not sure what is the confusion. Can you post some code to explain your point?
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Wed Feb 13, 2019 2:48 pm
by OCAJO1
Is not really a confusion, is more like - is not just
afterwards that works, So does
before.
Probably making a big deal out of nothing.

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Wed Feb 13, 2019 9:21 pm
by admin
"Before" doesn't work as illustrated by the code that I posted above.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Thu Feb 14, 2019 1:52 pm
by OCAJO1
I was going to let it go, but you stared it again
When you say "before" doesn't work, do you mean as for illustrating the effect of forward referencing? Because moving static int b; in your code to before the static block, makes everything compile fine like any other old code.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Thu Feb 14, 2019 9:54 pm
by admin

Well, it seems the point of this discussion has faded. Let me step back a bit. The book says :
A static block can access all static variables and static methods of the class. However, if the declaration of a static variable appears after the static block, then you can only set the value of that variable in the static block.
You seem to have a problem with this point, right? I think it is fine and the example given in the book illustrates it correctly. Do you have an example that disproves it?
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Fri Feb 15, 2019 12:58 pm
by OCAJO1
Wait a second, unless you are paraphrasing, I cannot find such a sentence in sec. 8.3.6 (at least not in build 9.0)
What I see is, "A static block can access all other static members of the class, i.e., static variables and static methods even if they appear after the static blocks. However, a static block can only set the value of a variable that is defined afterwards." which left things open to some interpretation, and started all this
"A static block can access all static variables and static methods of the class. However,
if the declaration of a static variable appears after the static block, then you can only set the value of that variable in the static block."
Given the underlined section of your quote, now it all makes sense.

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Fri Feb 15, 2019 9:04 pm
by admin
OK, I see the issue now. This statement was updated after similar feedback from another user to make it clearer.
Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]
Posted: Sat Feb 16, 2019 4:56 pm
by OCAJO1
Oh I did not see anything about it is the correction site, otherwise we wouldn't gone so many rounds of musical chairs about this.
