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 are the different data types in java?
Can abstract class have private constructor?
Explain creating threads by implementing runnable class?
What do you mean by JVM?
What do you mean by an interface in java?
What is the difference between heap and stack memory?
What are wrapper classes in java?
Why multiple inheritance is not supported by java?
What is difference between classpath and path variables in java?
What are accessor methods in java?
Can a class have multiple constructors?
How do you compare two objects?
How to compare strings in java?
What is io stream in java?
What is compiler and what its output.