| 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
|
|
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
|
 |
 |
|
|
subject: static blocks run at class loading
|
|
|