aspose 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 217

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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: 217

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://aspose.com/file-tools
 
subject: static blocks run at class loading