: Java Compilation error.

: Java Compilation error.

View Answers

November 1, 2008 at 10:06 AM

Hi friend,


Static block will execute when ever execution starts, it
means this block executes first of all, where as static
block have to call seperately and also it can call with out
object instance.


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();
}
}

static block is used to initialize the variables during the
JVM startup.
static methods are getting called with out creation of any
instance.

A STATIC block is automatically executed the first time the class where it is defined is called.
A Static Method is excuted only when a static call is made to this method.

For more information on java visit to :

http://www.roseindia.net/java/beginners/
http://www.roseindia.net/java/beginners/howtoaccessstaticmethod.shtml

Thanks

November 1, 2008 at 1:46 PM

static block is automatically executed when the program executes. And a static method u can directly call within the same class. In other class u can call using classname.method() without creating an instance...
Static block places lock for shorter periods in case of synchronization but static method places lock for longer









Related Tutorials/Questions & Answers:

Ads