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 Another qstn for you all... Just to enforce the point.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another qstn for you all... Just to enforce the point.." Watch "Another qstn for you all... Just to enforce the point.." New topic
Author

Another qstn for you all... Just to enforce the point..

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Another qstn for you all... Just to enforce the point..
I know, most of MUST be knowing this already. But recently I didn't find any discussions related to this. So I am posting this here.
regds
maha anna.
Which version of class Test compiles and runs successfully?
<pre>
class Test {
static int i = peek();
static int peek() { return j; }
static int j = 1;
public static void main(String[] args) {
}
}
class Test {
static int i = j;
static int peek() { return j; }
static int j = 1;
public static void main(String[] args) {
}
}
</pre>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Maha,
I tried your code with JVM. The first part compiles and runs fine while the second one gives compilation error,"Can't make forward reference to j".
I don't understand why both of them are behaving this way?
Your help will be much appreciated.
Thanks.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
The static/instance floating block initialization and class vars intializations are done in the SAME ORDER as they appear in a java program. The rule for this initialization is ,
1. The initialization expression on the RHS(RightHandSide) of any assignment statement SHOULD NOT DIRECTLY refer to another var which comes afterwards (below). Otherwise the compiler complains that it is a forward reference, which you can not do.
2. But acesses to class variables by methods are not checked in this way. You CAN do forward reference THROUGH methods. In this case the pre-defined value of the forwarded ref , which is the DEFAULT VALUE is used.
So in the foll. code peek() method returns the DEFAULT value of j which is 0 (not 1)is returned. Because the assignment of j comes BELOW the defn. of peek() method.
regds
maha anna
<pre>
class Test {
static int i = peek();
static int peek() {
return j;
}
static int j = 1;
public static void main(String[] args) {
}
}
</pre>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks a bunch Maha for detailed explanation.
 
Don't get me started about those stupid light bulbs.
 
subject: Another qstn for you all... Just to enforce the point..
 
Similar Threads
Forward Referencing
Question on static initializers..
Initializers...
Output or Compilation error? Surprising!
Why is it so?