what is the difference between static block and static
method

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When would you use a static class?

580


Is 0 true or false?

510


What is the default size of arraylist in java?

500


How do you check if a string contains only numeric digits?

615


Where is the find and replace?

528






What is difference between static and final?

556


What is a concrete classes? Is Java object class is concrete class?

510


What is class and object in java?

549


Is string passed by reference in java?

546


Explain the Propertie sof class?

586


What is command line used for?

587


Can we assign integer value to char in java?

509


Why string is not thread safe?

538


How are destructors defined in java?

570


What is the static variable?

571