what is the difference between static block and static
method

Answers were Sorted based on User's Feedback



what is the difference between static block and static method..

Answer / lolo

Static Block is executed when the Program starts.

Example :

public class StaticExample {

static{
System.out.println("Hello");
}

public static void main(String args[]){

}
}

When we run this program it will print Hello.

Static Methods are executed when those methods are called
from another static class or method

Example :

public class StaticExample {

static void printString(){
System.out.println("Hello");
}

static void testStaticMethod(){
printString();
}

public static void main(String args[]){
testStaticMethod();
}
}

Is This Answer Correct ?    134 Yes 4 No

what is the difference between static block and static method..

Answer / raghavendra

As soon as the class is loaded, the static block will be
executed. Where as the static method should be called
explictly.

Is This Answer Correct ?    51 Yes 4 No

what is the difference between static block and static method..

Answer / navaneesh

i think static block is executed, when the class is
loaded.That means at the time of compile time.so that
block sill executed first.

where as static method have to call seperately and also it
can call with out object instance.so it executed when run time.

Is This Answer Correct ?    56 Yes 20 No

what is the difference between static block and static method..

Answer / ravikiran

static block is used to initialize the variables during the
JVM startup.
static methods are getting called with out creation of any
instance.

Is This Answer Correct ?    34 Yes 6 No

what is the difference between static block and static method..

Answer / dharmendra

static block in the block that is used to initiate the class
members such as static int a;

This static block executed when the class loads first time
in memory and and thus it implicitly called by the compiler
not by the help of any one of the object.

class A
{
public A()
{
System.out.println("INSIDE THE CONSTRUCTOR OF THE A");
}

static
{
System.out.println("This is the static block");
}
}

class B
{
public static void main(String arr[])
{
A a = new A();
A b = new A();
}
}



the output of this program can be shown by the use of the
javac B.java
java B

the output generated is:

this is static block
INSIDE THE CONSTRUCTOR OF THE A
INSIDE THE CONSTRUCTOR OF THE A

this shows that the static block executed only ones as the
class loaded. and not at the time of the object created.



static method:

method is declared as static so that they can be called by
the name of the class.methodname();
these method usually not required any instance variable but
they can be use also with the objects.


and its loaded as many times as the object of the class is
created.


class A
{
static
{
System.out.println("This the static block");
}
static void print()
{
System.out.println("This is the class A static method");
}
}

class B
{
public satic void main(String arr[])
{
A a = new A();
A a1 = new A();
a.print(); // A.print();
a1.print(); // A.print();
}
}

the output of this method is :

this is the static block
This is the class A static method
This is the class A static method

Is This Answer Correct ?    22 Yes 2 No

what is the difference between static block and static method..

Answer / maruti

public static void main(string args[])
is also static method do u call this method?

Is This Answer Correct ?    33 Yes 14 No

what is the difference between static block and static method..

Answer / khasim

static block will be called at the time of loading the
class.it can be called once.developer can not be called
expecitly
static method will called at the time of loading the
class.here we can call expecitly by using there classname

Is This Answer Correct ?    16 Yes 1 No

what is the difference between static block and static method..

Answer / himanshu kumar upadhyay

Static block is executed when the class which contain static
block is loaded in to memory and static method is executed
when it is called. Mostly static block is used for
Initialization of static members.

Is This Answer Correct ?    17 Yes 2 No

what is the difference between static block and static method..

Answer / mahendra pratap singh

Static blocks execute first n than static method.It means
static block have high priority compare to static method

Is This Answer Correct ?    21 Yes 7 No

what is the difference between static block and static method..

Answer / kishore reddy

static{} blocks are executed on its own as soon as the
program starts. i.e before the main method is called where
as static() is called ONLY from another static method ie
main method.

static() has several restrictions:
1. Can ONLY call other static ()
2. MUST ONLY access static data
3. cant refer to super or this

Is This Answer Correct ?    14 Yes 2 No

Post New Answer

More Core Java Interview Questions

In Serialization, whether you will use Static variables?

3 Answers   HCL,


Hai all I want to print given array in reverse order Ex: int a[]={1,2,3,4,5};display this array in reverse order.

4 Answers  


What is the difference between reader/writer and inputstream/output stream?

0 Answers  


Can you pass by reference in java?

0 Answers  


What is a constructor, constructor overloading in java?

0 Answers  






Difference between Array and vector?

9 Answers  


what is the difference between Object Based Language and Object Oriented Language?

0 Answers   Aspire,


Differences between C and Java?

0 Answers   TCS,


what is meta-Inf?

2 Answers   Polaris,


Can a private method be declared as static?

0 Answers   Global Logic,


Why non nested classes in java are not having marked as protected access specifier

2 Answers   Google,


What is a bubble sort in java?

0 Answers  


Categories