Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 2 October 2016

In Java Which run first Static Block, Block, Constructor or Method() ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed the Program for 
What is memory leak in Java ?


The Most Important Question in terms of interview and in terms of  Individual Java Knowledge that what run first i.e constructor, static block, block, or method() . The Easiest way to know which run first is to write a Program which include a constructor, static block, block, and method().

public class WhichRunFirst{
 
public WhichRunFirst() {
System.out.println("constructor");
}
{
System.out.println("block");
}
static{
System.out.println("static block");
}
void go(){
System.out.println("method");
}
public static void main(String[] args)  {
WhichRunFirst a =new WhichRunFirst();
a.go();
}
}

Program Output :-

static block
block
constructor
method

As Output says that first Run Static Block then Block, Constructor and at last run User Defined Method(). If any one still have any doubt he can comment below.


Share this Blog with yours Friends !!

No comments:

Post a Comment