Saturday 23 December 2017

java - Static block execution before main method

itemprop="text">



Does static
block of class execute before the main method of same class?



Example:



public
class Example {


static
{

System.out.println("hi");
}

public static void
main(String[] args) {


System.out.println("bye");



}
}


The
output of this program is :





hi




bye





My
doubt is why the output was
not:





bye



class="post-text" itemprop="text">
class="normal">Answer



Ok, I got
the answer when i run the program using -- java example -- in command prompt. In
background java command executes main thread as -- example.main() -- which is similar to
static method calling.
So statements in the static block executes first than
my main method.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...