Static and Main

1.2K Views
0 Replies
1 min read
can anyone tell me that when we have static block in java then why do we need main function.

13 Replies

Without main, program is nothing.Code won't execute.

In every program,main is necessary.
Deven saw you after long time. Where were you?

Kha the bhai... kitne dino baad dekha..
Devan, A program start in java with main function only. static is second step to the program get execution
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!!!
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
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. :) :)
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. :) :)


Sandhya im not talking about static method here..im talking about static blocks...yes main is a static but static blocks are different...let me show with an example:

public class staticEx {
static {
System.out.println("Inside Static Block ");
}
}

this is a program without main in it but it still works with the help of an self executable block namely 'static block'....SO my ques was that if we have static block then why use main..and fyi static blocks are as equally efficient as main.

Also hoping you know this that the static block compiles before main
I will try later

Yes, static blocks, if exist in a pgm will compile before main.
I think, I have noted it here :dry:
Devan...
I have given this thread link to my friends and waiting for its reply. Hoping I can give it to you very soon

Topic Author

D

deven rana

@deven.rana

Topic Stats

Created Wednesday, 20 June 2012 23:07
Last Updated Tuesday, 30 November -0001 00:00
Replies 0
Views 1.2K
Likes 0

Share This Topic