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

What are some characteristics of interference class?

751


What is an accessor?

1310


What does replaceall do in java?

675


Define jre i.e. Java runtime environment?

770


How do we access static members in java?

827


How many bits is size_t?

723


How do you implement tree mirroring in java?

798


What is float in java?

757


Why do we use return statement?

742


Is special character in java?

799


How do you test a method for an exception using junit?

784


What do you mean by thread safe?

744


What is singleton class and how can we make a class singleton?

862


What is the difference between class & structure?

775


Which one will take more memory: an int or integer?

956