Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static blocks run at class loading

 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for having a look at my question.
Yes,Later i also found out the same after testing few codes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic