can anyone tell me that when we have static block in java then why do we need main function.
Like it on Facebook, Tweet it or share this topic on other bookmarking websites.
Without main, program is nothing.Code won't execute.

In every program,main is necessary.

Want to make each day Accountable

Deven saw you after long time. Where were you?

Kha the bhai... kitne dino baad dekha..

Want to make each day Accountable

Devan, A program start in java with main function only. static is second step to the program get execution

Slow and Steady Wins the Race.

thank you guys for your answers but i beg to differ with them..i dont know if you have tried it or not but if you make a program in jdk without main but with static block then it still gets compiled and executed without any error
Deven saw you after long time. Where were you?

Kha the bhai... kitne dino baad dekha..

ya bro was kind of busy with my studies and stuff
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!!!

Meera sandhu
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?
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?


Complete reference Java 2 :P

I got stuck somewhere..really forget it. Sorry...
I will give you answer..sure
Java is my favourite subject. That's why :P

Meera sandhu
ahem ahem,..im still waiting sandhya
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. :) :)

Meera sandhu
You do not have permissions to reply to this topic.