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 many java versions are there?
what is the role of xml in core java?? and how we can use it?? can somebody give a sample program with explanation and from where i can read more about xml?????
What is keyword auto for?
What is a byte array?
Write a code to create a trigger to call a stored procedure
Why main() method is public, static and void in java ?
How can we access some class in another class in java?
State the merge-sort principle and its time complexity.
How to create an interface?
When to use runnable interface vs thread class in java?
Should database connections be singleton?
What is an i/o filter?
Why we use protected in java?
What is the use of put method?
what is synchronization and why is it important? : Java thread