Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we execute anything before calling main method?

 
Nittin singla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Note the results are different in Java7


What is of different in Java7 that will change this behavior? thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a class which only contains a static block, and try to execute it in Java6 and Java7.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic