• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

how to start before the main starts

 
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!!!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
 
Marshal
Posts: 65814
250
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static initializer.

static {
//your code goes here...
}
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • 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
Marshal
Posts: 65814
250
  • 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.
 
Rancher
Posts: 3371
31
  • 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?
 
Ranch Hand
Posts: 300
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.
 
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)


 
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
Rancher
Posts: 3371
31
  • 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
Marshal
Posts: 65814
250
  • 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.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!