Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.629 :
Posted: Mon Dec 09, 2013 2:35 pm
by devlam
I don't get the last statement:
T is a top-level class, and an assert statement lexically nested within T is executed.
Is it possible to show a small clean example of that situation?
Re: About Question com.enthuware.ets.scjp.v6.2.629 :
Posted: Mon Dec 09, 2013 10:45 pm
by admin
Code: Select all
public class SomeClass{
static int j = 5;
static{
assert j == 5:"invalid"; //j will be 5 because the class will have been initialized before this statement is executed.
}
}
Basically, what it means is that a class or interface will be initialized before any assert statement that exists within the class is executed.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.629 :
Posted: Fri Dec 13, 2013 7:35 am
by devlam
The problem with the understanding I have is that if an assert statement is executed you are in the class so it's evidently the class is initialized.
In the example you give some of the other reasons for initializing must have happened before the static block will be "executed". What is wrong with my understanding?
Re: About Question com.enthuware.ets.scjp.v6.2.629 :
Posted: Fri Dec 13, 2013 8:47 am
by admin
Sorry, I am not really sure. You might want to consult JLS for further details.
-Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.629 :
Posted: Tue Apr 15, 2014 11:56 am
by cosmindumy
Explanation is right, but the correct answer is "super two" 3rd option, not 4th.