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
what is static import in java? Explain
How garbage collection is done in java?
Can we declare an interface as final?
Can static method access instance variables ?
Explain about main() method in java ?
Discuss 2D arrays.
What does the three dot emoji mean?
what is the significance of listiterator in java?
How will you get the platform dependent values like line separator, path separator, etc., ?
What is OOP Language?
Explain with example the concept of constant variable in java.
What is a parameter in matrices?
Explain about instanceof operator in java?
What is the purpose of lambda expressions?
What are inbuilt functions?