What is the difference between static variables and static block? which gets executed first?

Showing Answers 1 - 9 of 9 Answers

. When we declare a variable as static, it means that all objects of that class share this single variable. such variables are global variables.static block or say function means ,we can access that function, before any objects of its class are created,and without reference to any object

  Was this answer useful?  Yes

praveen kumar sakhamudi

  • Feb 9th, 2007
 

static variable is also variable but every object share same memory locaction of static variable.
static block is block it have set of statements.
static block is execute first bec when ever.
class file is loded jvm static block is executed.

praveen kumar sakhamudi
(+91-9949735117)

  Was this answer useful?  Yes

snehlata

  • Sep 24th, 2007
 

Static variables belongs to class only and they will execute when they are displayed in System.out.println() statement. By default they have a fixed value.
e.g
for integer 0
for float 0.0
Static block is executed before main() because it has no need of object to execute this block. As we run program then it will automatically run.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions