user2096592
user2096592

Reputation: 53

Execution of a Java Program starts from which line? Is it main method?

From where does execution of a java program starts? I heard people say its from main method. I think execution starts from static block. Am i right??

Upvotes: 1

Views: 19062

Answers (6)

Yogesh Kumar
Yogesh Kumar

Reputation: 1

If a class contains static block and main method () jvm Wil executive the static block first and then main method will be executed. But if you don't mention the static block in a class and the class contain only main method then jvm starts execution from main method only.

Upvotes: 0

Avi
Avi

Reputation: 21866

The main method is the entry point to your program. If the class that contains the "main" method has static members that need to be initialized or a static code block, this will be executed BEFORE the "main" method.

Look at this sample code:

public class Test {

    private static Object obj = new Object();

    public static void main(String[] args) {

        System.out.println("test");
    }

}

If you put a breakpoint in the object initialization line you will see it runs before the println line.

Upvotes: 2

A.H.
A.H.

Reputation: 66303

The output of this programm:

public class XXX {

    static YYY dependend = new YYY();

    static {
        System.out.println("3");
    }

    public static void main(String[] args) {
        System.out.println("4");
    }
}

class YYY {
    static {
        System.out.println("1");
    }
    YYY(){
        System.out.println("2");
    }
}

is, of course

1
2
3
4

So: The entry point for your program is main. But there is code executed before that. And there is no need that this "executed before main" code is in the same class. And there is also no need that this code is in a static initializer (see "2").

Upvotes: 4

Matt3o12
Matt3o12

Reputation: 4332

A Java application will be normally initialised by the main method:

public static void main(String... args){
    System.out.println("Executing my application...");
}

The static block will be executed when the JVM loads the class. You cannot start your application without a main method or the JVM will appear an error message.
It is theoretically possible to execute your code with a static block (example). But it is a bad way to initialise your application because the doSomethingElse method might be called by an other script, which doesn't want to create a gui (or whatever you do in your initialisation method). For example:

class Test2 extends Object{
    public static void doSomething(){
        System.out.println("Calling Test's doSomethingElse method.");
        Test.doSomethingElse();
    }
}

Test2's doSomething method wants only to call doSomethingElse but the method which creates the GUI is also called because the class Test is loaded by the JVM. When the JVM loads a class - and the class has got a static block - the static block will be called at fist. Test's static block calls now the executingClass method and the GUI will be created (but Test2 wants only to call doSomethingElse.

Finally, you shouldn't use a static block to initialise your application:

  1. The static block should only be used for creating stuff which is needed by that class.
  2. main method is also needed.
  3. It might have unpredictable effects.

Upvotes: 0

zch
zch

Reputation: 15278

The static blocks are executed when the class is initialized. For the class containing main method it will be before calling this method, because class has to be initialized before any of it's method is used. For other classes it can be later or never, if the class doesn't need to be initialized.

Upvotes: 0

TheEwook
TheEwook

Reputation: 11117

This is the start method of a java program:

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

Upvotes: 0

Related Questions