praise

S

Stefan Ram

This is really a page as good as documentation can be:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

. It explains all important aspects of the assert statement.

Then it ends with a huge FAQ section that really answers every
question one can possibly have about the assert statement.

One can even learn from it notions such as »package-tree
semantics« versus »traditional package semantics« or facts
such as »a class's constructors and methods can run prior to
its initialization« (»its« = »the class'«).

I think the exhaustive documentation of Java with the JLS,
the Java SE documentation, the tools' documentation, the
Java tutorial and such technotes is a strength of Java,
especially since it all comes from the owner of the Java
brand itself.
 
L

Lew

Stefan said:
One can even learn from it notions such as »package-tree
semantics« versus »traditional package semantics« or facts
such as »a class's constructors and methods can run prior to
its initialization« (»its« = »the class'«).

Strange. The JLS says:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4
"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 field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed."

So that contradicts.
 
M

markspace

Strange. The JLS says:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4
"A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
[the class is used]
So that contradicts.

The paragraph Stefan is referring to, I think, is:

" There is, however, one corner case that demands special treatment. It
is possible, though generally not desirable, to execute methods or
constructors prior to initialization. This can happen when a class
hierarchy contains a circularity in its static initialization."

So it looks to me like the JLS is implying a "best effort" to initialize
a class before it is used, but can't absolutely guarantee it in all cases.

It would be an interesting experiment to try to write some code to
reproduce the case where a ctor is called before its initialization.
 
M

Mike Amling

It would be an interesting experiment to try to write some code to
reproduce the case where a ctor is called before its initialization.

class Foo {
static Foo baz=new Foo();
static Object what=new Object();

Foo() {
System.out.println(what);
}
}

It's easy enough to run a constructor before class initialization is
complete. Offhand, I don't see how one would run a constructor before
class initialization begins.

Mike Amling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top