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
How variables are stored in memory?
What are the skills required for core java?
Explain about map interface in java?
I want my class to be developed in such a way that no other class (even derived class) can create its objects. How can I do so?
What will happen if non-synchronized method calls a static synchronized method and what kind of lock it acquires?
What are the differences between heap and stack memory in java?
What is a nonetype?
What is pass by value?
Which list is sorted in java?
How many bits is a float?
What is the difference between ArrayList and Vector? which one is better in Java
What is the declaration statement?
Why is string builder not thread safe?
How do you square a number in java?
What is string made of?