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

How we can run a jar file through command prompt in java?

0 Answers  


How many ways can an argument be passed to a subroutine and explain them?

0 Answers  


Is there a case when finally will not execute?

0 Answers  


What are the important features of Java 10 release?

0 Answers  


If I have 1000 objects and my requirement is to sort them quickly, then which collection would you recommend and why?

5 Answers   KPIT,






all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.

8 Answers   CFG,


Is boolean a data type in java?

0 Answers  


How many types of packages are there in Java?

1 Answers  


What is string variable?

0 Answers  


What is a blocking method in Java?

0 Answers  


What is an Exception ?

9 Answers  


Can we assign null to double in java?

0 Answers  


Categories