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
When would you use a static class?
Is 0 true or false?
What is the default size of arraylist in java?
How do you check if a string contains only numeric digits?
Where is the find and replace?
What is difference between static and final?
What is a concrete classes? Is Java object class is concrete class?
What is class and object in java?
Is string passed by reference in java?
Explain the Propertie sof class?
What is command line used for?
Can we assign integer value to char in java?
Why string is not thread safe?
How are destructors defined in java?
What is the static variable?