2

Is is possible to change entry point of java program from main(default) to other?

If I write code

public class TestWithoutMain {
    static {
        System.out.println("hello bristy!!!");
    }
}

I am not able to run code in eclipse. If i add main method to above code

public class TestWithoutMain {
    static {
        System.out.println("hello bristy!!!");
    }
    public static void main(String[] args) {

    }
}

It is printing hello bristy!!!.

Brajesh Kumar
  • 929
  • 6
  • 16
  • 4
    Why do you want to do this? – Karthik T Sep 03 '13 at 05:08
  • The point of the main function is that you put code in it, not in the `static { ... }` block – Karthik T Sep 03 '13 at 05:09
  • possible duplicate of [Having another static method as the entry point of the java program?](http://stackoverflow.com/questions/2472506/having-another-static-method-as-the-entry-point-of-the-java-program) –  Sep 03 '13 at 05:09
  • 1
    May be you should check this one : http://stackoverflow.com/questions/261428/entry-point-for-java-applications-main-init-or-run?rq=1 – java_enthu Sep 03 '13 at 05:10
  • static blocks are primarily used for loading external libraries with JNI. So, unless you are using JNI, you shouldn't need to use them. – Paul Nikonowicz Sep 03 '13 at 05:10
  • @PaulNikonowicz there are other use cases as well, such as doing complex static variable initializations. but that is not related to entry point need. – eis Sep 03 '13 at 05:14

2 Answers2

2

The basic concept is the main class is searched first and than and only than it is executed via main. So the first answer is NO. You cannot change the entry point.

Now in your code you have a static System.out.prinln block. In java, static contents are loaded when the class is loaded for the first time and they just have a single copy in the memory. So static block will be executed after the main block is found. Just try removing this main block and you will see the difference yourself

Umang Mehta
  • 1,467
  • 11
  • 16
2

Tricks like putting business code in static initializers (leaving main empty) are possible. But the primary purpose of static initializers is to perform some initializations, not running business code.

You may put something like System.out.println("TestWithoutMain class loaded"); for logging purposes, but this should not be the primary goal of your program.

Java does not prevent you from writing bad, unreadable and unmaintainable code. And from creating nonsense programs. Neither do other programming languages.

Common programming practice discourages you from putting business code in static initializers bypassing main.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • yes, but it could be an empty method defined in some `MainDummy` class used only to run the application. – Alex Shesterov Sep 03 '13 at 05:19
  • yeah. but if the question is "is it possible to change the entry point from main to something else", my answer would be no, as you always need the main method as entry point anyway. – eis Sep 03 '13 at 05:21
  • @eis: you are right; I rephrased the answer to avoid confusion. The other answer already says that changing entry point is not possible, so I won't repeat that. – Alex Shesterov Sep 03 '13 at 05:24