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 static blocks run at class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "static blocks run at class loading " Watch "static blocks run at class loading " New topic
Author

static blocks run at class loading

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 201

hi all,
I have come know that static blocks run when the class is loaded for the first time.But if this is the case ,then in the following code-

class supA
{
static{System.out.println("supA's static block");}
}
class supB extends supA
{
static{System.out.println("supB's static block");}
}
class supC extends supB
{
static{System.out.println("supC's static block");}
public static void main(String[]a)
{

}
}


when i try to run by 'java supC',it means i am loading class supC,isn't it,but why then the supA's static block ran first?then supB's and finally supC's?
also,does the super() call means that the immediate super class is loaded?


Please help me on this concept.
Any suggestions to improve the concept are welcome .

Thanks...
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2946
    
  15

The Super class is loaded even before the super() call.
The class loaded loads the class first time it encounters. So when it sees extends SomeClass, the classloader searches and loads the class.


Mohamed Sanaulla | My Blog
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 201

Thanks for having a look at my question.
Yes,Later i also found out the same after testing few codes
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static blocks run at class loading
 
Similar Threads
order?
When do classes get loaded into the JVM?
Question Regarding class loading precedence.
class getting loaded
Initialisation blocks