• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to start before the main starts

 
shantanu kaushik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to run a piece of code even before the main method is executed!!!
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you think?

Since this is a trick for entertainment purposes only, I shall move this thread to where we discuss such things.
 
Andrew Vershinin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static initializer.

static {
//your code goes here...
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What happens if you try that in Java7? Particularly if you miss out the main method?
 
Andrew Vershinin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Thanks
Campbell Ritchie wrote:What happens if you try that in Java7? Particularly if you miss out the main method?

1. AFAIK, Java 7 supports static initializers, so it will work as usual - after loading of the class.
2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.
Excuse me for my broken English
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what if you try it in Java6?
 
Andrew Vershinin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what if you try it in Java6?

The same thing.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, is there a reason why you think it might be different between 5 and 6?
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think static initializer Block are there from JDK 1.0 as its language feature not an API enhancement.
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Vershinin wrote:
Campbell Ritchie wrote:Welcome to the Ranch

2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.


This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)
 
shantanu kaushik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for all the details provided
 
Andrew Vershinin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viktor Kubinec wrote:
Andrew Vershinin wrote:
Campbell Ritchie wrote:Welcome to the Ranch

2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.


This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)

I tried:
Test.java


javac Test.java
java -cp . Test

Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Campbell, is there a reason why you think it might be different between 5 and 6?

I think Campbell was suggesting there's a difference between 6 and 7 and if this post is correct (I haven't tried it) then he is right.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that's probably what he's thinking of. But that post is about what happens if you try to execute a class with no main() method - which has been contrary to the spec all along. It's just that they've recently closed another loophole in the implementation. Whereas this thread here is about executing code before the main() method - which has been allowed, via class initializers, since the dawn of time. (Java time, that is.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what I was thinking of. I take the point that is not strictly what the question is about, however.
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Vershinin wrote:

This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)

I tried:
Test.java


javac Test.java
java -cp . Test

Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)




Ok, you are right. Sorry. It depends on java version. I'm using java 6, where the static initializer is executed even if the main method is not defined. As it seems in java 7 t is not a you have just shown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic