File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes FAQ - Traps 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 "FAQ - Traps" Watch "FAQ - Traps" New topic

FAQ - Traps

vidhya Ramachandran

Joined: Sep 15, 2000
Posts: 13

Can somebody give an eg for the following:
1.Forward references to variables gives compiler error.
2.Instance initializer(s) gets executed ONLY IF the objects are constructed.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
public class TestClass
 static{ System.out.println(" in static block"); }
 { System.out.println(" in non static block"); }
 public static void main(String args[])
  // i = 10; //will give compile time error.
  // int i;
  //new TestClass(); //"in non-static block" will not be printed.

Get Certified, Guaranteed!

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
1.Forward references to variables gives compiler error.
Paul gave an example.
To be nit-picky and confuse you more, I'll give an example when they do not give compiler error.

this code compiles because static vars are initizlized before instance vars, so forward reference instanceVar = staticVar is legal.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
I agree. Here's the link:
subject: FAQ - Traps
It's not a secret anymore!