This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static Code I've never seen!  RSS feed

 
marek duda
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
I am currently reading a lot of Javacode, and found sth. I ' ve never senn before:

how /when is Startup.configure executed?
someone seen something like this before?
Greetings
Marek
[ April 16, 2004: Message edited by: marek duda ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are looking at is a static block, or a static construction clause. What this means is that the code in the static block is run only once when an instance of the class is created, or when a static member of that class is accessed. So Startup.configure(Categories.SERVER_CATEGORY) is run the first time you use the JavaServer object, and never again.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called a static initialization block. It runs when the class is loaded into memory the first time.
Creating an instance of the class is not supposed to be necessary, when running on a properly written JVM. Apparently, some JVMs used to be written poorly, and they actually required a new instance of the class be created before the class was loaded into memory (and any static initialization blocks ran). (That's my understanding of why when loading a JDBC driver into memory, folks will typically use a command similar to Class.forName("jdbcDriverClass").newInstance();)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!