aspose file tools*
The moose likes Java in General and the fly likes Can we execute anything before calling main method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we execute anything before calling main method?" Watch "Can we execute anything before calling main method?" New topic
Author

Can we execute anything before calling main method?

Nittin singla
Greenhorn

Joined: Jul 02, 2011
Posts: 24
Hi,

This question was asked to me in some interview. How can we execute something before/without calling the main method.

TIA
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I suppose if you put something in the static initializer for the class containing the main method, it would execute before main().

It seems to me to be kind of a stupid interview question. Do they do a lot of this in this shop? Is it really important someone know this?

You could run an applet, or a servlet; then you'll never get to main().

rc
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
You can search for this, but as you have already been told, it is no more than a party trick. Note the results are different in Java7, so all the old replies are out of date.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Campbell Ritchie wrote:Note the results are different in Java7


What is of different in Java7 that will change this behavior? thanks


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Write a class which only contains a static block, and try to execute it in Java6 and Java7.
 
 
subject: Can we execute anything before calling main method?