sky
sky

Reputation: 140

difference between static block vs main method in java

my question is simple, why we need the main() method?. jvm call public static void main() method to start application. as well as static block also executed after classes loaded. without main() the program gives main method not found exception but you can avoid this error by adding System.exit(0) line at the static block after your statements, then whats the point of main() method?. is there any functionality's not available without main method?

class A{

public static void main(String ar[]){
.....
....
}

// VS

static{
....
...

System.exit(0);  // to avoid main method not found error

}

}

Upvotes: 1

Views: 1301

Answers (4)

Richard Tingle
Richard Tingle

Reputation: 17226

As mentioned there is the issue where command line arguments are impossible or needlessly difficult to access.

There are also the issue of programs with multiple mains in different classes. Sometimes I will create a separate main method to do initial testing of a new class. If I had to put that within the static initialisation block then every intended start point of the program would run whenever that class is loaded; this would be a new source of bugs for no benefit.

It also adds one more complexity of class initialisation that anything below the static initialisation block will have their default value rather than their set values. For example the following prints 0.

static{
    HackedStaticBlock h=new HackedStaticBlock();
}
static int badNumber=17;

public HackedStaticBlock() {
    System.out.println(badNumber);

}

This is something you sometimes need to worry about with static initialisation but this means you need to worry about it in every program.

Upvotes: 0

Solomon Slow
Solomon Slow

Reputation: 27190

The static block is not executed after the class has been loaded. It is executed while the class is being loaded. Until the static block exits, the class is not fully constructed.

-------- Edit ----------

@sky, try running this program, and see what happens:

class Foo {
    static String name = "Michael";
    static {
        Thread t = new Thread(new Bar());
        t.start();
        try {
            Thread.sleep(30000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
    }
}

class Bar implements Runnable {
    public void run() {
        System.out.println(Foo.name);
    }
}

Upvotes: 3

Abdul Fatir
Abdul Fatir

Reputation: 6357

Well I haven't given much thought to the question in scene however just by looking at the face of it, you cannot at least have command line arguments without a main() method.

Upvotes: 0

asteri
asteri

Reputation: 11592

Certainly you lose some functionality. How are you going to get the command-line arguments? There might be some clever trick to do so, but there's no need to make some hack for it.

The reason to have a standard main method across all applications is so that the JVM always knows where to look in a project for the block to begin execution. You have to have that standardized somewhere.

Upvotes: 3

Related Questions