File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes forName method with reference to the static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "forName method with reference to the static block" Watch "forName method with reference to the static block" New topic
Author

forName method with reference to the static block

Vishwadeep Aggarwal
Greenhorn

Joined: Oct 30, 2012
Posts: 3
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

Joined: Aug 07, 2007
Posts: 1939
    
  28
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

Joined: Oct 30, 2012
Posts: 3
Thanks.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forName method with reference to the static block
 
Similar Threads
Why do we do Class.forName(....) ?
static initializer setting parent variable
Attribute Setting failed
[newbie] IllegalAccessException
Use of Class.forName()