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


Please Help Members By Posting Answers For Below Questions

How are observer and observable used in java programming?

548


What is the difference between equals() and == in java?

531


What is the dot operator?

575


How can we pass argument to a function by reference instead of pass by value?

588


What is int short for?

519






Define Multiprogramming and Multiprocessing in java.

586


Why java is not 100% object-oriented?

837


What is jit compiler ?

580


How do I know if java is installed?

521


Why we used vector class?

636


What is ctrl m character?

519


What is t in generics in java?

565


Why collection doesn’t extend cloneable and serializable interfaces?

650


Is arraylist ordered in java?

580


Can we override constructors in java?

682