• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FAQ - Traps

 
vidhya Ramachandran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 }
}
HTH,
Paul.

------------------
Get Certified, Guaranteed!
http://www.enthuware.com/jqplus
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic