Static and Main
can anyone tell me that when we have static block in java then why do we need main function.
Just wait Devan
I have taken my Java text just now and it's in my hand.
I want to refer it so that I can answer your question.
Sure, I will do!!!
Wait!!!
which book?
can anyone tell me that when we have static block in java then why do we need main function.
can anyone tell me that when we have static block in java then why do we need main function.
Let me try to find an answer Devan....years have passed since i closed my books :P :P :P
Now also your question is not much clear to me. :blink: Yet, I will try. You can ask me, if still doubts exist.
main() itself is a static block.
Hoping you know it, without which we can't run a OOPS language. Also, all static blocks and variables are executed first before any other objects. Obviously that's why main() runs first while we run a program.
When we declare a variable or method as 'static', it can be accessed before any objects of its class are created and without reference to any object. That's its advantage.
Also all variables declared as static will be global variables too.
Your question is "when we have static block in java then why do we need main function"
So your answer is....main itself is a static block which runs first.
So, obviously a static block is running!!!!
Is your doubt cleared?
If not ask me. I will try to help you. :) :)
@deven.rana