This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FAQ - Traps  RSS feed

 
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: 4097
33
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!