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

Can a constructor be made final?

0 Answers  


How to implement an arraylist in java?

0 Answers  


how to handled exceptions & erros in ejb?

1 Answers   Satyam,


We are seeing so many videos/audios as many web sited. But question is these videos or audios are stored in Databases ( Oracle, Mysql, Sybase,... ) or stored any file directory from there they will give the link for that? Pls explain and give sample code to achieve this one? Thanks, Seenu.

0 Answers   Symphony,


why we need this (1.object,2.class,3.data hiding,4.encapsulation,5.abstraction,6. polymorphism,7.inheritance)

2 Answers  






What is intern method in java?

0 Answers  


What is a databasemetadata?

0 Answers  


What is mean by collections in java?

0 Answers  


What is the difference between logical data independence and physical data independence?

0 Answers  


How many techniques can be employed to create a string object?

0 Answers  


What are methods and how are they defined?

0 Answers  


How can we achieve thread safety in java?

0 Answers  


Categories