Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Tell some latest versions in JAVA related areas?

0 Answers  


what is default constructor and parameterised constructor with example?

1 Answers   KPIT,


What is a lightweight component?

0 Answers  


Using callable statement how can you pass out parameters, explain with example?

0 Answers  


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

0 Answers  


WHAT IS THE MEANING OF ALL TYPE OF BUZZWORDS?

4 Answers  


Why we used vector class?

0 Answers  


What is the mapping mechanism used by java to identify IDL language?

0 Answers  


Does java map allow duplicates?

0 Answers  


Are true and false keywords?

0 Answers  


how come we know the object is no more used in the class?

2 Answers   Accenture,


What JNDI(Java Naming and Directory Interface) provides?

4 Answers  


Categories