[HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

[HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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);
	}
}

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post by flex567 »

aha I see.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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."

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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.

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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?

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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. :)

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post by admin »

"Before" doesn't work as illustrated by the code that I posted above.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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.

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post by admin »

:D
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?

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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. :joy:

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post by admin »

OK, I see the issue now. This statement was updated after similar feedback from another user to make it clearer.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 193, Sec. 8.3.6 - class-loading-and-static-initializers]

Post 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. :)

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests