Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

forName method with reference to the static block  RSS feed

 
Vishwadeep Aggarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun has provided one forName method as

forName(String cName, boolean b,ClassLoader load)

Q: My question is if i will pass the boolean parameter as false then is there any chance that the static block of the class which is going to be loaded using forName method will not going to be executed. Also what is the actual purpose of the second boolean parameter in the forName(String cName, boolean b,ClassLoader load) method?
 
Tony Docherty
Bartender
Posts: 3270
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If b is false then the loaded class will not be initialized unless it has previously been loaded and initialized. So if forName is called and the class hasn't been previously initialized then the static initializer block(s) will not be called but it will get called when you first attempt to use the class.
It's purpose is so you can load a class without having to immediately initialize it - ie you delay the initialization until a future time when the class is first used.
 
Vishwadeep Aggarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!