File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion and class initialization 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 "Assertion and class initialization" Watch "Assertion and class initialization" New topic
Author

Assertion and class initialization

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125

Run the program as: java -da CircularB
The output is:
Assertions are On
Assertions are Off
I am not quite understand that 'assertions are temporarily on during initialization'.
Also, as I understood, the sequence of this program is as:
1. before the main, CircularB needs to be initialized.
2. before CircularB is initialized, CircularA needs to be initialized.
3. While initializing CircularA, a method of CircularB will be called.
So my question is, will this sequence be endless -B invoke A, A invoke B...and forever?
And one more stupid question, what on earth the initialization do?
Thanks for your helps!


SCJP 1.4
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
one thing that may be confusing you:

Your assert statement does not have a logic test, it has an assignment of true to the variable assertionsOn.
Bill
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi,
What i understood is that the rules pertaining to assertions are applied only after the class's initialization. Since CircularA is loaded and initialzed before CircularB assertionon=true gets evaluated and you get the output assertionon ON. The call of CircularB.report doesn't force CircularB's initialization. I didn't know taht before so i checked in JLS and the rules about class initialization are (this answers your second
question) :

12.4.1 When Initialization Occurs
Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.

What's weird though, according to SUN By default, assertions are disabled at runtime and if you try to run the code above whithout any switch you get the same result.
You can verify what i said in the beginning by forcing CircularB to be intialized before CircularA. Here's an exemple:


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
See El
Ranch Hand

Joined: Oct 07, 2002
Posts: 44
Originally posted by Yan Bai:
Run the program as: java -da CircularB
The output is:
Assertions are On
Assertions are Off
I am not quite understand that 'assertions are temporarily on during initialization'.

I'm confused too !!! .
Shouldn't it be :
Assertions are Off
Assertions are Off
as assertion is disabled ?

Also, as I understood, the sequence of this program is as:
1. before the main, CircularB needs to be initialized.
2. before CircularB is initialized, CircularA needs to be initialized.
3. While initializing CircularA, a method of CircularB will be called.
So my question is, will this sequence be endless -B invoke A, A invoke B...and forever?

I think the sequence is as you've described above and there's no circular initialization invoked.


See El<br />SCJP2, SCWCD, IBM XML, SCBCD
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125

Your assert statement does not have a logic test, it has an assignment of true to the variable assertionsOn.

Yes, it's an assignment.
But I run the program with assertion disabled, and the result is as:

Assertion is On
Assertion is Off

From the explanation (sorry I couldn't paste the whole text here since I just printed it out and couldn't figure out it's URL now) -- the assertion is temporarily turned on during the class initialization not matter the real desire is (even you intentionally turn it off on the command line).
My question is - Sorry for the length
1.Whether there's a recursive initialization: B call A, A call B, and B call A again, no end. (A and B represent CircularA and CircularB)
2.What are the initialization include? I know that when a class is loading, the following sequence is executed:
a. static block and variable initialized
b. class block and variable initialized
c. constructor called
Then, how about a function call? like in this example, when A call B.report(). My opinion is B needs to be initialized BEFORE report() can be called. So when the report() gets called, B has finished it's initialization process, the same as the report() call in main. Thus, the result should be:
Assertion is Off
Assertion is Off
I must lost something here, but how do I know which call is during the initialization, and which is after?
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Originally posted by Yan Bai:

Then, how about a function call? like in this example, when A call B.report(). My opinion is B needs to be initialized BEFORE report() can be called. So when the report() gets called, B has finished it's initialization process, the same as the report() call in main. Thus, the result should be:

I must lost something here, but how do I know which call is during the initialization, and which is after?

Read the Quote u mentionned up.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Oops sorry i meant I mentionned up
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Shishio, Thanks a lot, I am reading your post now

[ October 26, 2002: Message edited by: Yan Bai ]
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125

I got the result as:
Assertions are Off

SubClass Start ...
SupClass Start ...
Assertions are On //1
Assertions are Off //2
SupClass Finish ...
Static do it
SubClass Finish ...
Assertions are Off //3

From the code, report() are explicitly called twice.
3 is obviously generated from the report() in main. But how could both 1 and 2 are printed out? are there totally 3 calls on report()? I thought that only 1 should be generated as the result of CircularB.report() (line1 in code block) again
[ October 26, 2002: Message edited by: Yan Bai ]
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
hmm... Are u sure.
The output i get is:

SubClass Start ...
SupClass Start ...
Assertions are Off
SupClass Finish ...
Static do it
SubClass Finish ...
Assertions are Off
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Oops...My fault... I keep the old CircularB.java and saved yours as CircularB2.java, but forgot to change CircularB.report() to CircularB2.report()
I think I got the idea now
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look here.
Note specially the following:
If an assert statement executes before its class is initialized, as in the above example, the execution must behave as if assertions were enabled in the class.

[ October 26, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Yan I guess there's something wrong, I took your code, compiled it, ran it and it output:
---------------
SubClass Start ...
SupClass Start ...
Assertions are Off
SupClass Finish ...
Static do it
SubClass Finish ...
Assertions are Off
---------------
No Assertions are ON at all in the output, what kind of compiler/JVM are you using? I'm using Sun's JDK 1.4


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Originally posted by Alfred Kemety:
Yan I guess there's something wrong, I took your code, compiled it, ran it and it output:
---------------
SubClass Start ...
SupClass Start ...
Assertions are Off
SupClass Finish ...
Static do it
SubClass Finish ...
Assertions are Off
---------------
No Assertions are ON at all in the output, what kind of compiler/JVM are you using? I'm using Sun's JDK 1.4

This output is expected Alfred: CircularB is initialized first.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Originally posted by Yan Bai:
[QB]
Print out:

Assertions are On
Assertions are Off

The reason for the 1st line output is because assertion is temporarily on DURING the class initialization.

public class CircularB3{
static {
boolean assertionsOn = false;
assert assertionsOn=true; //1
System.out.println("Assertions are "+(assertionsOn?"On":"Off"));
}
static public void main(String [] arg) {}
}

print out:

Assertions are Off

But line 1 is in the static block, it is still IN the initialization process, so I thought that the assertion should be turned on at this time. What wrong with my thoughts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion and class initialization