Hi....
Ques : I want to print "Hello" even before main() is executed. How will you achieve that? What are the restriction imposed on a static method or a static block of code?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 3, 2012, 11:23 PM
Print the statement("Hello") inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.
Thanks