aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A qstn to thnk and answer - an old discussion reraised Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A qstn to thnk and answer - an old discussion reraised" Watch "A qstn to thnk and answer - an old discussion reraised" New topic
Author

A qstn to thnk and answer - an old discussion reraised

Hari Sree
Greenhorn

Joined: Jan 18, 2005
Posts: 2
Hi Everybody,

This is regarding the discussion posted earlier in 2000 by Maha Anna.
I am a new visitor to this site and I am quiet impressed by
the discussions. I am preparing for SCJP.
I tried to compile this code in JDK1.3 and J2SDK1.4.2_02.
The code is provided below for u'r reference:

interface inter
{
static final int INTER_ONE = base.one();
}

class base
{
static final int ONE = one();
static int one()
{
System.out.println("base.one() called");
return 1;
}
}

class sub extends base implements inter
{
static
{
System.out.println("static block of sub called");
}
/* static int one()
{
System.out.println("sub.one() called");
return 10;
}*/

}
class test
{
static
{
System.out.println("static block of test called");
}
public static void main(String[] args)
{
System.out.println(sub.one());
}
}


For the code with comments the output was as follows:

In JDK1.3:

static block of test called
base.one() called
base.one() called
1

In J2SDK1.4.2_02 :

static block of test called
base.one() called
static block of sub called
base.one() called
1

For the code without comments the output was same in both the compilers:

static block of test called
base.one() called
static block of sub called
sub.one() called
10

I think there is difference in the way the JVM in these two compiler versions
treat the same piece of code. Can anyone please help me in understanding this.

regds,
Sree.JDK1.3J2SDK1.4.2_02
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
From the JLS, section 12.3:
This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the language are respected, that a class or interface is completely verified and prepared before it is initialized, and that errors detected during linkage are thrown at a point in the program where some action is taken by the program that might require linkage to the class or interface involved in the error.

For example, an implementation may choose to resolve each symbolic reference in a class or interface individually, only when it is used (lazy or late resolution), or to resolve them all at once while the class is being verified (static resolution). This means that the resolution process may continue, in some implementations, after a class or interface has been initialized.


This question involves the sequence of class loading, which in turn controls the execution of static initialization blocks. According to the JLS, this is not fully specified. So the differences you observed are normal.


Mike Gershman
SCJP 1.4, SCWCD in process
Hari Sree
Greenhorn

Joined: Jan 18, 2005
Posts: 2
Thanks Mike!

If there is a difference in the output of the execution in two versions, which one to follow while preparing for the SCJP?
Also Where can I find the Language Specification for J2SDK1.4.2_02?
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
Check this...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A qstn to thnk and answer - an old discussion reraised